Resolve a URL from a Partial View (ASP.NET MVC)
来源:互联网 发布:男士学java好还是ui好 编辑:程序博客网 时间:2024/05/20 21:58
Working on an ASP.NET MVC application and needed the ability to resolve a URL from a partial view. For example, I have an image I want to display, but I need to resolve the virtual path (say, ~/Content/Images/New.png) into a relative path that the browser can use, such as ../../Content/Images/New.png or /MyAppName/Content/Images/New.png.
A standard view derives from the System.Web.UI.Page class, meaning you have access to the ResolveUrl and ResolveClientUrl methods. Consequently, you can write markup/code like the following:
<img src='=<%=Page.ResolveClientUrl("~/Content/Images/New.png")%>' />
The problem is that the above code does not work as expected in a partial view. What's a little confusing is that while the above code compiles and the page, when visited through a browser, renders, the call to Page.ResolveClientUrl returns precisely what you pass in, ~/Content/Images/New.png, in this instance. The browser doesn't know what to do with ~, it presumes it's part of the URL, so it sends the request to the server for the image with the ~ in the URL, which results in a broken image.
I did a bit of searching online and found this handy tip from Stephen Walther - Using ResolveUrl in an HTML Helper. In a nutshell, Stephen shows how to create an extension method for the HtmlHelper class that uses the UrlHelper class to resolve a URL. Specifically, Stephen shows how to add an Image extension method to HtmlHelper. I incorporated Stephen's code into my codebase and also created a more generic extension method, which I named ResolveUrl.
1
public
static
MvcHtmlString ResolveUrl(
this
HtmlHelper htmlHelper,
string
url)
2
{
3
var urlHelper =
new
UrlHelper(htmlHelper.ViewContext.RequestContext);
4
return
MvcHtmlString.Create(urlHelper.Content(url));
5
}
With this method in place you can resolve a URL in a partial view like so:
<img src='<%=Html.ResolveUrl("~/Content/Images/New.png")%>' />
Or you could use Stephen's Html.Image extension method (although my more generic Html.ResolveUrl method could be used in non-image related scenarios where you needed to get a relative URL from a virtual one in a partial view). Thanks for the helpful tip, Stephen!
Determining Whether a String Is Contained Within a String Array (Case Insensitive)
if
(stringArrayName.Contains(
"valueToLookFor"
, StringComparer.OrdinalIgnoreCase))
2
...
3
else
4
...
- Resolve a URL from a Partial View (ASP.NET MVC)
- Display a view from another controller in ASP.NET MVC
- How to use jquery or ajax to update razor partial view in c#/asp.net for a MVC project
- ASP.NET MVC Partial View 和 Child Action
- ASP.NET MVC 3 (Adding a View) (3/9)
- Can we call the Method of a controller from another controller in asp.net MVC?
- ASP.NET MVC 3 (Accessing your Model's Data from a Controller) (5/9)
- [ASP.NET MVC 小牛之路]12 - Section、Partial View 和 Child Action
- [ASP.NET MVC 小牛之路]12 - Section、Partial View 和 Child Action
- [ASP.NET MVC 小牛之路]12 - Section、Partial View 和 Child Action
- ASP.NET MVC 3 (Adding a Create Method and Create View ) (6/9)
- asp.net mvc AjaxBeginForm结合Partial使用
- A great book for asp.net mvc
- Generate PDF documents from a HTML page using ASP.NET
- A Complete URL Rewriting Solution for ASP.NET 2.0(转)
- A Complete URL Rewriting Solution for ASP.NET 2.0
- ASP.NET MVC URL Routing
- asp.net mvc css url
- PHP中this,self,parent的区别
- HDU2767 Proving Equivalences 图的强联通性
- Ubuntu上ftp安装配置
- URL编码问题
- 《团队之美》的那些作者们
- Resolve a URL from a Partial View (ASP.NET MVC)
- a place nearby
- 养成良好的习惯
- HP-UX 命令 及用法【转帖】
- System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本
- HP-UX 命令 及用法【转帖】
- 使用oracle的olap函数优化分页查询
- 解决error:2014 Commands out of sync; you can't run this command now
- oracle数据库的优化-关于rownum操作