3

I'm using an OWIN-based command line Self-Host app, and the new attribute routes work great for my Web API controllers. However, they don't appear to work for my regular MVC controllers.

I've tried mapping the routes the old way with a route template in my start method, and I've also tried doing it with attribute routes. Either way, I get 404 when I try to hit those routes.

In my start code I call the IAppBuilder.UseWebApi(...) extension method defined in Owin.WebApiAppBuilderExtensions class, but I didn't see an equivalent for MVC, something like UseMvc(...). Can they coexist? Am I missing something obvious?

Here's my start code:

var config = new HttpConfiguration();
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(Startup.OAuthOptions.AuthenticationType));
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.MapHttpAttributeRoutes();

app.UseCookieAuthentication(CookieOptions);
app.UseExternalSignInCookie(CookieAuthenticationDefaults.ExternalAuthenticationType);
app.UseOAuthBearerTokens(OAuthOptions, ExternalOAuthAuthenticationType);
app.UseFacebookAuthentication(appId: "12345", appSecret: "abcdef");
app.UseWebApi(config);

And here's my plain old MVC class:

[RoutePrefix("Home")]
public class HomeController : Controller
{
    [HttpGet("Index")]
    public ActionResult Index()
    {
        return Content("Hello world!", "text/plain");
    }
}

When I hit /Home/Index on my app, I get 404.

superjos
  • 12,189
  • 6
  • 89
  • 134
gzak
  • 3,908
  • 6
  • 33
  • 56

2 Answers2

4

Currently ASP.NET MVC doesn't run on OWIN. Web API was built more recently and with this type of flexibility in mind so it's been decoupled from System.Web, specifically HttpContext, which allows it to run on OWIN.

Some alternatives that do run on OWIN are FubuMVC, Nancy and Simple.Web

David Fowler has done some work on this and has a somewhat working prototype of MVC running on OWIN but the code hasn't been made public and there's been no word on if it will be any time soon.

Brian Surowiec
  • 17,123
  • 8
  • 41
  • 64
1

Ensure you have NuGet package "WebApp API" installed for AttributeRouting.

Similar question: MVC Attribute Routing Not Working

Community
  • 1
  • 1
Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
  • Close, but I'm actually using the attribute routing system which ships with ASP.NET 5.0.0-RC1 (part of Visual Studio 2013 RC), so it's not quite the same. – gzak Oct 02 '13 at 02:22