简体   繁体   English

如何在MVC4中使用带有参数的Url.Content()?

[英]How can I use Url.Content() in MVC4 with parameters?

Here is my code: 这是我的代码:

@Url.Content("~/AspNetWebForms/ViewPDF.aspx?id=" + docID.ToString() + "&small=True")

I am dynamically building my Manifest file for Application Caching in MVC4. 我正在为MVC4中的应用程序缓存动态构建我的Manifest文件。 I know it's on the way out (or appears to be) but I do still need to support older browsers. 我知道它正在出路(或似乎是),但我仍然需要支持旧的浏览器。

The site is in MVC, but we do have a couple ASPX pages because we need to use some legacy controls. 该站点位于MVC中,但我们确实有几个ASPX页面,因为我们需要使用一些传统控件。 So in my manifest file I am trying to create a relative path to the ASPX pages using Url.Content(), which seems to work until I need to add a couple parameters and the "&" is encoded. 所以在我的清单文件中,我试图使用Url.Content()创建ASPX页面的相对路径,这似乎有效,直到我需要添加一些参数并且“&”被编码。 Without the "&" it seems to work (except that it loads/caches the wrong thing). 没有“&”它似乎工作(除了它加载/缓存错误的东西)。

And since it's a manifest file, I cannot do a redirect, as any redirect causes the application cache to fail. 由于它是一个清单文件,我无法进行重定向,因为任何重定向都会导致应用程序缓存失败。

Even though it's MVC4, I cannot just start with the "~" because it doesn't get parsed as it would in an image or anchor. 即使它是MVC4,我也不能只用“〜”开头,因为它不像在图像或锚中那样被解析。

Just append the query string to the output of Url.Content instead. 只需将查询字符串附加到Url.Content的输出Url.Content

@(Url.Content("~/AspNetWebForms/ViewPDF.aspx) + "?id=" + docID.ToString() + "&small=True")

or 要么

@string.Format("{0}?id={1}&small=true", 
    Url.Content("~/AspNetWebForms/ViewPDF.aspx"), docID.ToString())

At first I thought it was the Url.Content that was encoding the value, but it's actually the Razor engine doing it. 起初我以为是编码值的Url.Content,但它实际上就是Razor引擎。 Html.Raw corrected it, though I did have to add an empty line afterwards as the line break was lost, too. Html.Raw纠正了它,虽然我之后必须添加一个空行,因为换行也丢失了。

@Html.Raw(Url.Content("~/AspNetWebForms/ViewPDF.aspx?id=" + docID.ToString() + "&small=True"))

Paul Abbott's answer lead me to this conclusion so all thanks to him. Paul Abbott的回答引导我得出这个结论,所以都要感谢他。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM