0

I have added a Web API feature to an ASP.NET MVC app and want to use attribute routing. When running the site in Visual Studio 2015, I can enter URLs in the browser address bar and get the methods on my controller to work. Once I move the site to IIS, all I get is 404 responses. I have tried many code changes with no luck.

The BuilderApiController file is in a WebAPI/v1/Controllers folder. Will attributes on the controller be found here?

namespace Hds.Edsi.WebAPI.V1.Controllers
{
    [System.Web.Http.RoutePrefix("api/v1/builder")]
    public class BuilderApiController : BaseApiController
    {
        [System.Web.Http.Route("GetExternalOrganizationID/{envisionOrgID}")]
        [System.Web.Http.HttpGet]
        public HttpResponseMessage GetExternalOrganizationID(string envisionOrgID)
        {

WebApiConfig.cs file. Standard stuff. If I am using attribute routing, do I need the config.Routes.MapHttpRoute part? I have tried it with and without.

If I need it, what is the purpose of config.MapHttpAttributeRoutes()? When I look at GlobalConfiguration,Configuration._routes after Register is called, none of the attribute routing defined routes are there. Should they be?

namespace Hds.Edsi.WebAPI
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API routes
            config.MapHttpAttributeRoutes();

            /*
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/v1/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
            */

protected void Application_Start()
{
    GlobalConfiguration.Configure(WebApiConfig.Register);
    AreaRegistration.RegisterAllAreas();
    GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
    GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new StringEnumConverter());
    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);

    Components.Automapper.CreateMaps();

    // Requrired for managing AppDomain teardown - IRegisteredObject
    MonitorManager.RegisterInstance();
}

IIS Express:

http://localhost:8181/api/v1/builder/GetExternalOrganizationID/123.123.123.123.1234 returns a JSON object as I would expect.

IIS:

http://my server/api/v1/builder/GetExternalOrganizationID/123.123.123.123.1234 returns a 404

I am guessing that I am missing something simple here or don't understand how attribute routing works.

Added

Here is the system.webServer section from web.config:

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true">
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
      <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" />
      <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" />
    </modules>
  <handlers>
    <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
    <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*" verb="*" type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />

    <remove name="OPTIONSVerbHandler" />
    <remove name="TRACEVerbHandler" />
  </handlers>
  </system.webServer>

1 Answer 1

1

As per as i know, if it's not working properly on your IIS, then that is an IIS error, not an ASP.NET error so this doesn’t actually come from ASP.NET’s routing engine but from IIS’s handling of expressionless URLs.

You can try with adding runAllManagedModulesForAllRequests to your web.config

<system.webServer>
     <modules runAllManagedModulesForAllRequests="true">
        <add name="ScriptCompressionModule" type="Westwind.Web.ScriptCompressionModule,Westwind.Web" />
      </modules>
</system.webServer> 

In detail you can refer Rick Strahl's Web Log ASP.NET Routing not working on IIS 7.0

Hope this helps!

Sign up to request clarification or add additional context in comments.

3 Comments

I added the system.webServer section from my web.config. Seems OK per the article you linked to.
So, what? does this working now or having same problem
Put a trailing forward slash on the URL and it all works as intended. The dots in the parameter were causing IIS to try and map to a file which doesn't exist so 404. Forward slash at the end lets IIS know this is not a specific file as so the routing works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.