【发布时间】:2013-04-22 08:27:18
【问题描述】:
当使用 AttributeRouting 而不是 WebAPI 使用的默认 RouteConfiguration 时,如何设置要使用的默认控制器。 即去掉注释代码部分,因为这在使用 AttribteRouting 时是多余的
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 应用程序,我会收到以下错误,因为没有定义默认的 Home 控制器/操作。 HTTP 错误 403.14 - 禁止 Web 服务器配置为不列出此目录的内容。
如何通过属性路由为 Home 控制器/动作指定路由?
编辑:代码示例:
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 属性的控制器操作?
-
@Pete:我已经更新了帖子以包含代码示例
标签: c# asp.net-web-api attributerouting