Allow browser to hit a .cshtml file and display the content

不羁岁月 提交于 2019-12-12 15:27:56

问题


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"); 
        }
    }


来源:https://stackoverflow.com/questions/18237168/allow-browser-to-hit-a-cshtml-file-and-display-the-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!