csharp asp.net mvc路由的绝对URL

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp asp.net mvc路由的绝对URL相关的知识,希望对你有一定的参考价值。

using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace oclockvn.Extensions.Web
{
    public static class UrlExtensions
    {
        public static string ContentAbsolute(this UrlHelper urlHelper, string contentPath, bool toAbsolute = true)
        {
            var path = urlHelper.Content(contentPath);
            var url = new Uri(HttpContext.Current.Request.Url, path);

            return toAbsolute ? url.AbsoluteUri : path;
        }

        public static string ActionAbsolute(this UrlHelper url, string actionName)
        {
            Uri requestUrl = url.RequestContext.HttpContext.Request.Url;
            return url.Action(actionName, null, null, requestUrl.Scheme, null);
        }

        public static string ActionAbsolute(this UrlHelper url, string actionName, object routeValues)
        {
            Uri requestUrl = url.RequestContext.HttpContext.Request.Url;
            return url.Action(actionName, null, new RouteValueDictionary(routeValues), requestUrl.Scheme, null);
        }

        public static string ActionAbsolute(this UrlHelper url, string actionName, RouteValueDictionary routeValues)
        {
            Uri requestUrl = url.RequestContext.HttpContext.Request.Url;
            return url.Action(actionName, null, routeValues, requestUrl.Scheme, null);
        }

        public static string ActionAbsolute(this UrlHelper url, string actionName, string controllerName)
        {
            Uri requestUrl = url.RequestContext.HttpContext.Request.Url;
            return url.Action(actionName, controllerName, null, requestUrl.Scheme, null);
        }

        public static string ActionAbsolute(this UrlHelper url, string actionName, string controllerName, object routeValues)
        {
            Uri requestUrl = url.RequestContext.HttpContext.Request.Url;
            return url.Action(actionName, controllerName, new RouteValueDictionary(routeValues), requestUrl.Scheme, null);
        }

        public static string ActionAbsolute(this UrlHelper url, string actionName, string controllerName, RouteValueDictionary routeValues)
        {
            Uri requestUrl = url.RequestContext.HttpContext.Request.Url;
            return url.Action(actionName, controllerName, routeValues, requestUrl.Scheme, null);
        }

        public static string ActionAbsolute(this UrlHelper url, string actionName, string controllerName, object routeValues, string protocol)
        {
            Uri requestUrl = url.RequestContext.HttpContext.Request.Url;
            return url.Action(actionName, controllerName, new RouteValueDictionary(routeValues), protocol, null);
        }
    }
}

以上是关于csharp asp.net mvc路由的绝对URL的主要内容,如果未能解决你的问题,请参考以下文章

csharp ASP.NET MVC 5表单

csharp asp.net mvc模块属性

csharp ASP.NET MVC WebViewPageExtensions

csharp 典型的ASP.NET MVC捆绑类

csharp ASP.NET MVC和LINQ

csharp ASP.NET MVC - 表单输入数组