Allow browser to hit a .cshtml file and display th

2019-07-04 05:19发布

问题:

I know this is a very odd request but to cut a long story short a developer have the wrong URL that has now been printed on material. The url was a .cshtml file which obviously is not allowed to be hit through IIS.

I need to allow this particular cshtml or all of them to be rendered as plain html in the browser.

Help will be appreciated.

回答1:

This might not NOT the best solution, but it is the one I know of the top of my head.

Go to your Global.asax file. From there go inside of or create the Application_AcquireRequestState function as so:

void Application_AcquireRequestState(object sender, EventArgs e) { }

Inside the above function check to see if the path matches your .cshtml file. If so, do Server.Transfer to a regular aspx page.

You might also have to go into IIS settings and enable cshtml to be served.



回答2:

You can use the IIS mod_rewrite extension with a regex for all cshtml files, or just this particular one.

http://www.iis.net/downloads/microsoft/url-rewrite

OR, in the IIS manager use the MIME Types configuration and add .cshtml as type text/html



回答3:

You could use the Application_BeginRequest event to check the extension of the file and apply some logic then redirect them.

In Global.asax.cs file:

protected void Application_BeginRequest()
    {
        if (Request.Url.AbsolutePath.EndsWith(".cshtml"))
        {
            //Your logic to apply
            Response.RedirectToRoutePermanent("Default"); 
        }
    }