Allow browser to hit a .cshtml file and display th

2019-07-04 05:32发布

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.

3条回答
看我几分像从前
2楼-- · 2019-07-04 05:46

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"); 
        }
    }
查看更多
虎瘦雄心在
3楼-- · 2019-07-04 05:50

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

查看更多
SAY GOODBYE
4楼-- · 2019-07-04 06:06

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.

查看更多
登录 后发表回答