如何在使用AttributeRouting而不是WebAPI使用的默認RouteConfiguration時設置默認控制器。 即擺脫了註釋代碼段,因爲這個使用AttribteRouting使用AttributeRouting指定WebAPI中的默認控制器/操作路由
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//routes.MapRoute(
// name: "Default",
// url: "{controller}/{action}/{id}",
// defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
//);
}
}
如果我評論上面的部分,並嘗試運行的WebAPI應用時是多餘的,我得到以下錯誤,因爲沒有默認的首頁控制器/行動定義。 HTTP錯誤403.14 - 禁止 Web服務器配置爲不列出此目錄的內容。
如何通過屬性路由爲主控制器/操作指定路由?
編輯:代碼示例:
public class HomeController : Controller
{
[GET("")]
public ActionResult Index()
{
return View();
}
public ActionResult Help()
{
var explorer = GlobalConfiguration.Configuration.Services.GetApiExplorer();
return View(new ApiModel(explorer));
}
}
既然你寫道你正在使用AttributeRouting,你能否在你的問題中提供一個代碼示例,顯示一個控制器動作標記爲AttributeRouting屬性? – 2013-06-13 19:04:53
@Pete:我已更新該帖子以包括代碼示例 – 2013-06-18 05:39:10