Jonathan
Jonathan

Reputation: 711

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

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.

Upvotes: 3

Views: 2854

Answers (3)

Justin
Justin

Reputation: 3395

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

Upvotes: 0

TheCatWhisperer
TheCatWhisperer

Reputation: 981

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.

Upvotes: 2

Neil Neyman
Neil Neyman

Reputation: 2158

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

Upvotes: 0

Related Questions