2013-10-01 30 views
3

我使用的是基於OWIN的命令行自託管應用程序,新的屬性路線我的Web API控制器工作的偉大。但是,他們似乎不適合我的常規MVC控制器。屬性路線MVC控制器不解決

我試圖映射舊的方式,在我的啓動方法的路由模板的路線,而且我也試圖與屬性的路由做。無論哪種方式,當我嘗試擊中這些路線時,我都會得到404。

在我的開始代碼中,我調用Owin.WebApiAppBuilderExtensions類中定義的IAppBuilder.UseWebApi(...)擴展方法,但是我沒有看到MVC的等價物,就像UseMvc(...)。他們可以共存嗎?我錯過了明顯的東西嗎?

這裏是我開始代碼:

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

這是我的老式MVC類:

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

當我打/Home/Index我的應用程序,我得到404

回答

1

確保您有爲AttributeRouting安裝的NuGet包「WebApp API」。

類似的問題:MVC Attribute Routing Not Working

+0

關閉,但我實際使用屬性路由系統附帶的ASP.NET 5.0.0-RC1(的Visual Studio 2013 RC的一部分),所以它是不太一樣的。 – gzak

4

目前ASP.NET MVC不上OWIN運行。 Web API是最近建立的,考慮到這種類型的靈活性,所以它與System.Web,特別是HttpContext分離,允許它在OWIN上運行。

那些在OWIN運行一些替代品FubuMVCNancySimple.Web

David Fowler做了一些這方面的工作,並MVC對OWIN運行的有些工作原型,但代碼還沒有被公開,有如果它很快就會發生的話,我們一直沒有說過。