asp.net-mvc ASP.NET MVC 中的 Page.ResolveUrl 相当于什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2452931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the equivalent to Page.ResolveUrl in ASP.NET MVC?
提问by Mark Redman
What is the equivalent to Page.ResolveUrl in ASP.NET MVC available in the Controller?
控制器中可用的 ASP.NET MVC 中的 Page.ResolveUrl 相当于什么?
回答by Atanas Korchev
It is Url.Content:
它是Url.Content:
ASPX:
ASPX:
<link rel="stylesheet" href="<%= Url.Content("~/Content/style.css") %>" type="text/css" />
Razor:
剃刀:
<link rel="stylesheet" href="@Url.Content("~/Content/style.css")" type="text/css" />
回答by Carlo
This should do what you're looking for...
这应该做你正在寻找的......
System.Web.VirtualPathUtility.ToAbsolute("~/")
回答by KyleMit
Here are a whole bunch of ways to resolve a path that uses that application root operator (~)
以下是解决使用该应用程序根运算符 ( )的路径的一大堆方法~
UrlHelper.ContentHttpServerUtility.MapPathWebPageExecutingBase.HrefVirtualPathUtility.ToAbsoluteControl.ResolveUrl
UrlHelper.ContentHttpServerUtility.MapPathWebPageExecutingBase.HrefVirtualPathUtility.ToAbsoluteControl.ResolveUrl
To call any method with inline code on an asp.net page, the method either needs to be exposed as an instance variable on the current object, or available as a static/shared method.
要在 asp.net 页面上使用内联代码调用任何方法,该方法要么需要作为当前对象的实例变量公开,要么作为静态/共享方法可用。
A typical MVC page gives us access to quite a few of these as properties via the WebViewPage. Ever wonder when you type @ViewData, you get magically wired up to the ViewData? That's because you have hit a property exposed by the MVC page you're on.
典型的 MVC 页面让我们可以通过WebViewPage. 有没有想过当你输入 时@ViewData,你会神奇地连接到 ViewData?那是因为您遇到了您所在的 MVC 页面公开的属性。
So to call these methods, we don't necessarily refer to the type they represent, but the instance property that exposes them.
因此,要调用这些方法,我们不一定要引用它们所代表的类型,而是要引用公开它们的实例属性。
We can call the above instance methods like this (respectively):
我们可以像这样(分别)调用上面的实例方法:
href="@Url.Content("~/index.html")"
href="@Server.MapPath("~/index.html")"
href="@Href("~/index.html")"
We can do this to call a shared method that doesn't need an instance:
我们可以这样做来调用一个不需要实例的共享方法:
href="@VirtualPathUtility.ToAbsolute("~/index.html")"
AFAIK, an MVC page doesn't automatically create an instance of anything from the System.Web.UI namespace, from which ResolveUrlinherits. If, for some reason, you really wanted to use that particular method, you could just new up a control and use the methods it exposes, but I would highly recommend against it.
AFAIK,MVC 页面不会自动从 System.Web.UI 命名空间创建任何ResolveUrl继承的实例。如果出于某种原因,你真的想使用那个特定的方法,你可以新建一个控件并使用它公开的方法,但我强烈建议不要使用它。
@Code
Dim newControl As New System.Web.UI.Control
Dim resolvedUrl = newControl.ResolveUrl("~/index.html")
End Code
href="@resolvedUrl"
That all said, I would recommend using @Url.Contentas it fits best with MVC paradigms
说了@Url.Content这么多,我建议使用它,因为它最适合 MVC 范式
回答by Rahul Ranjan
UrlHelper.Content()does the same thing as Control.ResolveUrl().
UrlHelper.Content()做同样的事情 Control.ResolveUrl().
For Further References: http://stephenwalther.com/archive/2009/02/18/asp-net-mvc-tip-47-ndash-using-resolveurl-in-an-html.aspx
更多参考:http: //stephenwalther.com/archive/2009/02/18/asp-net-mvc-tip-47-ndash-using-resolveurl-in-an-html.aspx
回答by Carter Medlin
回答by Paul Creasey
Server.MapPath() //returna full path
or
或者
url.content()
回答by Ashish Gupta
try using Server.MapPath().
尝试使用 Server.MapPath()。

