简体   繁体   中英

IIS url rewrite ignores .chtml ( Razor ) files

I have a rewrite rule that says.

site.one/content/templates/whatever.cshtml -> site.two/content/templates/whatever.cshtml

The problem occurs when site.one trying to fetch .cshtml files. It returns a 404. However if i change the extension to .js or .html then the rule works.

If i use the site.two link the request handles it just fine, this is without any rewriterules ofcourse. site.one can also handle .cshtml files from within its own directory.

Iam using IIS 10, Urlrewrite module 2, and Application Request Routing 3.

UPDATE

So the .cshtml in this case are template files that is fetched with async text.js, and then gets compiled with handlebars as the last step before rendered, but some of them need to get processed by razor engine because of the translations. This works if not rewriting.

Razor files are never served by the MVC framework or IIS. Razor files are processed by MVC and the result of the processing is sent to the output through a controller action method. The server is correctly responding with a 404 response in this case, because these files are blocked by default (you would never want to serve an unprocessed .cshtml file with source code to the user).

Since these files are blocked by IIS, there is no way to redirect them using the URL Rewrite Module (nor should there be).

I am guessing that you probably want to redirect your MVC URLs, which don't have an extension by default. The default way they are accessed (which can be customized) is /{controller}/{action}/ . For example, the in the MVC template the default way to get to the about page is through the URL /Home/About/ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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