Abh*_*tel 8 c# asp.net-web-api attributerouting
如何设置使用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 }
//);
}
}
Run Code Online (Sandbox Code Playgroud)
如果我评论上面的部分并尝试运行webapi应用程序,我会收到以下错误,因为没有定义默认的Home控制器/操作. 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));
}
}
Run Code Online (Sandbox Code Playgroud)
您是否尝试过以下方法:
//[RoutePrefix("")]
public class HomeController : Controller
{
[GET("")]
public ActionResult Index()
{
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
这将在集合中添加路径,其中url模板为"",控制器和操作的默认值分别为"Home"和"Index".
这对我有用。将其添加到课堂Register()上WebApiConfig
config.Routes.MapHttpRoute(
name: "AppLaunch",
routeTemplate: "",
defaults: new
{
controller = "Home",
action = "Get"
}
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19998 次 |
| 最近记录: |