首页 文章

从ASP.NET MVC Action获取绝对URL

提问于
浏览
67

这可能是一个虚拟问题,但我找不到明确的指示 . 我在MVC3 Web应用程序中有一个POCO类,其唯一目的是管理服务器中某些文件的备份 . 通常,它会创建一个备份并将文件名返回给控制器,控制器会发送一封电子邮件,其中包含用于下载它的URL . 这工作正常,但我无法构建要发送的绝对URL . 无论我使用哪种功能,我总是得到一个相对的URL,比如/Backup/TheFile.zip,而不是像http://www.somesite.com/Backup/TheFile.zip . 我试过了:

VirtualPathUtility.ToAbsolute("~/Backup/SomeFile.zip");
HttpRuntime.AppDomainAppVirtualPath + "/Backup/SomeFile.zip";
Url.Content("~/Backup/SomeFile.zip");

但它们都返回类似/Backup/SomeFile.zip的东西 . 任何的想法?

7 回答

  • 4

    您可以通过以下方式完成此操作:

    var urlBuilder =
        new System.UriBuilder(Request.Url.AbsoluteUri)
            {
                Path = Url.Action("Action", "Controller"),
                Query = null,
            };
    
    Uri uri = urlBuilder.Uri;
    string url = urlBuilder.ToString();
    // or urlBuilder.Uri.ToString()
    

    您可以使用 Url.Content() 或任何路由方法,或者只是传递路径,而不是此示例中的 Url.Action() .

    但是,如果URL确实转到 Controller Action ,则有一种更紧凑的方式:

    var contactUsUriString =
        Url.Action("Contact-Us", "About",
                   routeValues: null /* specify if needed */,
                   protocol: Request.Url.Scheme /* This is the trick */);
    

    这里的技巧是,一旦在调用任何路由方法时指定 protocol / scheme,就会得到一个绝对URL . I recommend this one when possible ,但在第一个示例中您也有更通用的方式,以备不时之需 .

    我在这里详细介绍了它:
    http://gurustop.net/blog/2012/03/23/writing-absolute-urls-to-other-actions-in-asp-net-mvc/

    摘自Meligy的AngularJS和Web Dev Goodies时事通讯

  • -4

    从控制器内部:

    var path = VirtualPathUtility.ToAbsolute(pathFromPoco);
    var url = new Uri(Request.Url, path).AbsoluteUri
    
  • 105

    这对我有用:

    using System;
    using System.Web;
    using System.Web.Mvc;
    
    public static class UrlExtensions
    {
        public static string Content(this UrlHelper urlHelper, string contentPath, bool toAbsolute = false)
        {
            var path = urlHelper.Content(contentPath);
            var url = new Uri(HttpContext.Current.Request.Url, path);
    
            return toAbsolute ? url.AbsoluteUri : path;
        }
    }
    

    在cshtml中的用法:

    @Url.Content("~/Scripts/flot/jquery.flot.menuBar.js", true)
    
  • 0

    如果 hostprotocol 参数非空,则MVC 4中的内置帮助程序会创建绝对URL . 请参阅this answer here,并在视图中使用示例扩展方法 .

  • 1

    在ASP.Net Core 2.0(MVC)中,这可以创建一个动作的绝对URL .

    var url = Url.Action("About", "Home", new { /*Route values here*/ }, Request.Scheme);
    
  • 35

    我为此编写了一个帮助类,对于MVC 5 ......它非常灵活,如果你不在控制器内部需要这个功能,它特别有用 . 您应该能够将其直接放入项目并继续 .

    正如Meligy指出的那样,关键是要包括协议 . 在这里,我将其硬编码为http,因此如果您想使用可能需要变得更灵活的SSL .

    public class AbsoluteUrlHelper
    {
        /// <summary>
        /// Creates an absolute "fully qualified" url from an action, and assumes the current controller.
        /// </summary>
        /// <returns></returns>
        public static string GetAbsoluteUrl(string action, object routeValues = null)
        {
            var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
            var values = urlHelper.RequestContext.RouteData.Values;
            var controller = values["controller"].ToString();
    
            return GetAbsoluteUrl(action, controller, urlHelper, routeValues);
        }
    
        /// <summary>
        /// Creates an absolute "fully qualified" url from an action and controller.
        /// </summary>
        public static string GetAbsoluteUrl(string action, string controller, object routeValues = null)
        {
            var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
    
            return GetAbsoluteUrl(action, controller, urlHelper, routeValues);
        }
    
        /// <summary>
        /// Creates an absolute "fully qualified" url from an action and controller.
        /// </summary>
        public static string GetAbsoluteUrl(string action, string controller, UrlHelper urlHelper, object routeValues = null)
        {
            var uri = urlHelper.Action(action, controller, routeValues, "http");
    
            return uri;
        }
    }
    
  • 17

    你有几个选择:

    • 将HttpContext.Request.Url的值保存在静态或成员变量中,并使用它来传递完全限定路径 .

    • 将应用程序域保存在web.config中的应用程序设置中 .

    • 硬编码值 .

相关问题