IIS url rewrite ignores .chtml ( Razor ) files - asp.net-mvc

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/.

Related

MVC .Net Redirect to remote unhosted html file

I am trying to redirect to an unhosted html file in an MVC action. The page returns with corrupted content error. I am trying to redirect like this:
return Redirect("file:///C:/test/mytestfile.html");
This of course works fine if the file is on a web server:
return Redirect("http://myserver/mytestfile.html");
Is it even possible to use the file protocol when redirecting in MVC? I've also tried:
return new RedirectResult("file:///C:/test/mytestfile.html");
and
Response.Redirect("file:///C:/test/mytestfile.html");
The project I'm doing this in is a bridge solution to overcome some shortcomings in a vendor solution, so unfortunately I can't just move the target files to web server. I really need to redirect to the file on the share where it lives.
This is not supported and is not a limitation of ASP.NET MVC, it is how web browsers work. You cannot redirect to the file:/// protocol if the web application is hosted on a web server (http://). You can only redirect to file:/// if the page that is redirecting is also hosted on file:///.
More info on the subject can be found here and the rules are defined here.
You could possibly have the website do a file read on the HTML file and then output the file to the page. This might be a start. You would end up with a wrapper "page" on your site that mirrored the remote HTML file.

ASP.net mvc How to handle 404 which caused by missing of file

I have file in folder "/UploadDir/TextFile1.txt"
I need:
download file if it exists
if file not exists (for example url :"/UploadDir/TextFile2.txt") should be shown custom errors message.
But now my mvc application not handle any requests to missed or existing files, when i place breackpoint on Application_BeginRequest and try to request text file - nothing occurred.
Besides i tryed to override HttpNotFound and HandleUnknownAction but same unsuccessfully.
Who can help me how to hande request to file if it exists or show custom error if it not found.
ps. route configuration is default
It sounds like the request GET /UploadDir/TextFile1.txt is being handled by IIS and not being passed into the ASP.NET pipeline. IIS will attempt to serve the content using its static file handler due to the .txt extension, and discover the file does not exist, issuing a 404.
You need to change your configuration to make these requests be passed to the ASP.NET handler for further processing. The exact details of that depend on your version of IIS.

asp mvc 2 route all requests to files in specific folder to given controller/action

Can't make this work fast. I have a folder in which I have text files.
And so if user requests some file like mysite.com/thefolder/file.txt he sees the contents of the file.
What route should I register so that all requests to anything in the folder would go to mysite.com/Error/NotAllowed action?
IIS will, by default, serve all .txt files directly without even going to the ASP.NET handler, which handles routing and MVC.
To change that behaviour, you will have to change your IIS settings or change the routing rules at any firewall/load balancer you have in front of it.

asp.net mvc routing, ignore route with extension in the middle of url

Hi How can make the asp.net routing engine ignore routes with an extension of the type
/pathtofile/filename.aspx/morepaths
I know this is hardly a real scenario but I need to know for another similar issue for an autogenerated url
Thanks
The MVC routing engine will not intercept a url if there is a matching file on the file system. (See RouteCollection.Ignore Method) So your example url will work fine. Query strings will also work fine.
You can test this out as follows:
Create an MVC application in Visual Studio
Run it
In your browser enter the url of the Site.css file in the Contents folder.
The file will be served and the browser will pop up the "Save" dialog.
Create an html file anywhere on the site and enter the url.
Your browser will display the html page.
Create an aspx web form anywhere on the site and enter the url.
Your browser will display the web form.
Add a query string or additional path to the url.
Your browser will display the web form.
You can also do this with .asp (classic ASP) pages (although the VS web server won't serve .asp pages, you have to set the site up in IIS for that to work.)
I hope that answers your question.

Flash movie not made available by ASP.NET MVC

I'm working on an ASP.NET MVC app that uses a Flash movie as a banner. I'm trying to load it, however for some reason the url gets treated as an Action and the user is redirected to the login page.
The funny thing is, if I put a file in the same directory with a different extension (.txt, for example) and try to load that, it doesn't get treated as an Action.
If I point my browser to this url:
http://localhost/AppImWorkingOn/Content/Banner/banner.swf
That gets me a redirection to /Account/LogOn?ReturnUrl=..., the standard ASP.NET MVC login route.
If I point my browser to this url:
http://localhost/AppImWorkingOn/Content/Banner/banner.txt
That gets me the file. Both urls point to files existing on the server in the same directory. It's as if something is blocking the .swf filetype.
I'm using IIS on Windows 7, and checked MIME and such, but can't find the problem. How does ASP.NET MVC decide whether something's a file or a controller action?
Make MVC ignore the route for the ".swf" extension.
http://haacked.com/archive/2008/07/14/make-routing-ignore-requests-for-a-file-extension.aspx
The issue was file permissions. This was a file that came from a zip sent to me via e-mail. I dragged the folder out of the zip and moved it into place, which was probably the worst I could do. The files were individually blocked and encrypted (they turned up green) so the UrlRouteModule wouldn't treat them as files that existed and would instead insist that they're Action methods.
I should have "unblocked" the zip file before starting to copy the assets in it.

Resources