Home How to TransferRequest to cshtml file?
Reply: 3

How to TransferRequest to cshtml file?

Michal Hosala
1#
Michal Hosala Published in 2018-01-05 16:38:20Z

I am looking at the ASP.NET MVC4 application which is calling Server.TransferRequest("Error.cshtml") while executing one of the requests where Foo.cshtml is a view page residing in the root folder of the application. Everything is working fine on our old server, but once I deployed this app to our new 2012r2 server with IIS8 I am getting following error page:

Description: The type of page you have requested is not served because it has been explicitly forbidden. The extension '.cshtml' may be incorrect. Please review the URL below and make sure that it is spelled correctly.

Requested URL: /Error.cshtml

Why is that? How to make TransferRequest work? What setup to perform on new server? To reproduce you can quite simply create a new ASP.NET MVC5 project, add Error.cshtml razor page to its folder, then in the Index action of HomeController call HttpContext.Server.TransferRequest("Error.cshtml") and preview, you should get the same error as above.

Once I put a breakpoint in Application_Error method in Global.Asax.cs and preview the app, I get an exception:

Message: Path '/WebApplication1/Error.cshtml' is forbidden. at System.Web.HttpForbiddenHandler.ProcessRequest(HttpContext context) at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Also, if I deploy mew ASP.NET MVC5 test app calling the TransferRequest to our old server (server 2008), it is still not working and throws an exception... So it seems like both old server and the original ASP.NET MVC4 app are somehow magically configured to make this thing work...

Emilio Ceroleni
2#
Emilio Ceroleni Reply to 2018-01-05 23:44:11Z

Pleas check if .cshtml is listed as a MIME type in your old server for the application's context. If this is the case, then you can (against recommendation) add .cshtml as a MIME type in the application's context. You could add a MIME type on a higher level, such as site or server and your application's context should inherit it but it is not recommended, unless you need to share the definition between multiple applications.

DISCLAIMER Giving users direct access to .cshtml is not considered best practice. If Error.cshtml is intended to be served as static content then you should rename it and rewrite it as HTML. If you set .cshtml as an allowed MIME type then you may be allowing users to inspect every .cshtml file in your web application. Alternatively you could write a controller to serve the content you need, for example:

public ActionResult CustomError()
{
   return View("Error");
} 
....   
HttpContext.Server.TransferRequest("CustomError")

Though I would recommend to use Exception Filters

Tommy
3#
Tommy Reply to 2018-01-05 18:41:30Z

By default, a new MVC 5 application generates a web.config file with the following lines:

Root web.config file

<configuration>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>

Views folder web.config

  <appSettings>
    <add key="webpages:Enabled" value="false" />
  </appSettings>

When the webpages:Enabled is set to false, rendering of .cshtml and .vbhtml files directly are prevented by the application. Change this value to true, save and recompile. Your application should now render these files.

Michal Hosala
4#
Michal Hosala Reply to 2018-01-10 11:33:21Z

In the end the root cause of the problem is rather silly.. Yes, you have to have webpages:Enabled set to true in web.config, but that is the default value in MVC4 anyway. However, the difference lies in the url:

  • localhost/WebApp results into error about cshtml pages being forbidden
  • localhost/WebApp/ renders just fine

So the difference has to be somewhere in routing, which I am not going to investigate further as it is buried in HttpServerUtility.TransferRequest. Both of the requests hit the HttpContext.Server.TransferRequest("Error.cshtml") breakpoint, but result is different.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.314044 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO