简体   繁体   English

重写URL时,@ Url.Content和@Href无法正常工作

[英]@Url.Content and @Href not working correctly when URL is rewritten

@Url.Content and @Href helper methods in ASP.NET MVC are not working correctly in one situation. 在一种情况下,ASP.NET MVC中的@ Url.Content和@Href帮助程序方法无法正常工作。 This is situation when on localhost, and it is hosted in IIS rather than on IIS Express so URL looks like something: http://localhost/MyApp/index-hr instead of for example http://localhost:12345/index-hr . 这是在localhost上的情况,它托管在IIS中而不是IIS Express上,因此URL看起来像: http:// localhost / MyApp / index-hr而不是例如http:// localhost:12345 / index-hr This is how I call my script: 这就是我调用脚本的方式:

   <script src='@Href("~/Scripts/toastr/toastr.js")' type="text/javascript">       </script>

I also tried: 我也尝试过:

    <script src='@Url.Content("~/Scripts/toastr/toastr.js")' type="text/javascript"></script>, 

and: 和:

     <script src='~/Scripts/toastr/toastr.js' type="text/javascript"></script>

It resolves Url great except situation when URL is something like : http://localhost/MyApp/index-hr . 它可以很好地解决Url的问题,除非URL类似于: http://localhost/MyApp/index-hr My URL /index-hr is rewritten and maps to controller Home , and action HomeIndex . 我的URL /index-hr被重写并映射到控制器Home和操作HomeIndex I noticed that it resolves OK when URL is http://localhost/MyApp/Home , so maybe it is not problem because of extra slash(when app is hosted on local IIS) but because URL is rewritten. 我注意到,当URL为http://localhost/MyApp/Home时,它可以解析为OK,所以这也许不是问题,因为存在额外的斜线(当应用程序托管在本地IIS上时),但是因为URL被重写了。 Other pages with rewitten URL load toast.js OK but they have deeper links , but in this situation it resolves toast.js to this URL: http://localhost/Scripts/toastr/toastr.js which result in 404 ofcourse. 其他URL格式不正确的页面也可以加载toast.js,但它们具有更深的链接,但是在这种情况下,它将toast.js解析为以下URL: http://localhost/Scripts/toastr/toastr.js ,结果为404。 It should resolve to: http://localhost/MyApp/Scripts/toastr/toastr.js 它应该解析为: http://localhost/MyApp/Scripts/toastr/toastr.js

I am not sure if I found solution or just fixed this thing and messed other things but as suggested here I wrote this code is global.asax: 我不确定是否找到了解决方案,还是只是解决了这个问题并弄乱了其他东西,但是按照这里的建议我编写的代码是global.asax:

   protected void Application_BeginRequest()
    {

        Context.Items["IIS_WasUrlRewritten"] = false;
    }

It says here that Tilde (~) notation maps to the original URLs by using IIS URL rewrite in ASP.NET Web Pages Razor V3. 这里说的是,Tilde(〜)表示法通过使用ASP.NET Web Pages Razor V3中的IIS URL重写映射到原始URL。 In case URL will not work in future here is explanation: 如果网址将来无法使用,请在此处进行说明:

This issue occurs because the behavior of tilde notation in URLs is changed in Web Pages Razor V3 for consistency with ASP.NET MVC. 发生此问题的原因是,为了与ASP.NET MVC保持一致,Web页Razor V3中更改了URL中的波浪符号的行为。 In ASP.NET MVC, the tilde notation in the Url.Content method or the Html.ActionLink method produces the original URLs regardless of the IIS URL rewrite rules. 在ASP.NET MVC中,无论IIS URL重写规则如何,Url.Content方法或Html.ActionLink方法中的代字号都会产生原始URL。

However, in Web Pages Razor V2, the tilde notation in URLs maps to the rewritten URLs when the IIS URL rewriting module is enabled. 但是,在Web Pages Razor V2中,启用IIS URL重写模块时,URL中的波浪号表示法会映射到重写的URL。 For example, when the requests under content.asp.net are rewritten to the URL under asp.net/content/, the href attribute in is resolved to /content/book/. 例如,将content.asp.net下的请求重写为asp.net/content/下的URL时,href属性解析为/ content / book /。 In Web Pages Razor V3, the same href attribute is translated into /book/, which is the original URL in the browser. 在网页Razor V3中,相同的href属性被转换为/ book /,这是浏览器中的原始URL。

It was better in razor v2 than in razor v3? 在razor v2中比在razor v3中更好?

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

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