Showing posts with label ASP.NET MVC. Show all posts
Showing posts with label ASP.NET MVC. Show all posts

Thursday, May 7, 2015

ASP.NET MVC 5 custom errors

ASP.NET MVC 5 allows you to define custom errors in the config file, similar to the following:

  <system.webServer>
    <httpErrors errorMode="Custom" existingResponse="Replace">
      <remove statusCode="404"/>
      <remove statusCode="500"/>
      <error statusCode="404" responseMode="ExecuteURL" path="/Error/NotFound" />
      <error statusCode="500" responseMode="ExecuteURL" path="/Error/ServerError" />
    </httpErrors>
  </system.webServer>

This usually works, but depending on the version of IIS, you may also need to set TrySkipIisCustomErrors on the Response.

public ActionResult NotFound()
{
    Response.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
    Response.TrySkipIisCustomErrors = true;

    return View();
}


Friday, July 25, 2014

Create web.config transform based on publish profile

If you've used Visual Studio since 2010 or 2012, you know how easy it is to transform config files based on the build configuration.

If you've used SlowCheetah, you know how easy it is to transform config files (other than web.config) based on the build configuration OR Visual Studio publish profiles.

You probably also know how unintuitive it can be to add a web.config transformation based on the project's publish profiles. Here's how.

From an existing profile







Right-click and choose "Add Config Transform", not "Add Transform".