MVC5中的Attributerouting无法正常工作

编程入门 行业动态 更新时间:2024-10-28 18:35:06
本文介绍了MVC5中的Attributerouting无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

I'm trying to use AttributeRouting but it seems like it is not going to work at all.

我的尝试:

What I have tried:

I have a navigation page(angularJS controller) from which it goes to a specific MVC controller: $scope.Department = function () { window.location.href = appSetting.publish + "/Settings/Departments"; } There is a `SettingsController` which is: public class SettingsController : Controller { public ActionResult Departments() { return View(); } } This works perfectly until I tried using this: $scope.Department = function () { window.location.href = appSetting.publish + "/appsettings/Departments"; } and the controller to this: [RoutePrefix("appsettings")] public class SettingsController : Controller { [Route("Departments")] public ActionResult Departments() { return View(); } } It goes to Http error 404 not found page. I have referred this article(www.c-sharpcorner/UploadFile/b1df45/web-api-route-and-route-prefix-part-2/). Also in global.asax class and added the following line of code: GlobalConfiguration.Configure(WebApiConfig.Register); And in `WebApiConfig`: public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Attribute routing. config.MapHttpAttributeRoutes(); // Convention-based routing. config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } } And in `RouteConfig`: public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapMvcAttributeRoutes(); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }); } } I have all these. I checked if I have AttributeRouting (ASP.NET MVC) alongside AttributeRouting (ASP.NET Web API) in NuGet package and both were not installed so I just tried with AttributeRouting (ASP.NET MVC) but it got me the same error.

推荐答案

scope.Department = function(){ window.location.href = appSetting.publish +/ Settings /部门; } 有一个`SettingsController`是: 公共类SettingsController:Controller { public ActionResult部门() {返回View(); } } 这在我尝试使用之前完美无缺: scope.Department = function () { window.location.href = appSetting.publish + "/Settings/Departments"; } There is a `SettingsController` which is: public class SettingsController : Controller { public ActionResult Departments() { return View(); } } This works perfectly until I tried using this:

scope.Department = function(){ window.location.href = appSetting.publish +/ appsettings / Departments; } 和控制器: [RoutePrefix(appsettings)] public class SettingsController:Controller { [Route(Departments)] public ActionResult Departments() { return View(); } } 转到Http错误404找不到页面。 我已经推荐过这篇文章(www.c-sharpcorner/UploadFile/b1df45/web-api-route-and-route-prefix-part-2/)。 同样在global.asax类中添加以下代码行: GlobalConfiguration.Configure(WebApiConfig.Register); 在`WebApiConfig`中: 公共静态类WebApiConfig { public static void Register(HttpConfiguration config) { //属性路由。 config.MapHttpAttributeRoutes(); //基于约定的路由。 config.Routes.MapHttpRoute( name:DefaultApi, routeTemplate:api / {controller} / {id}, defaults:new {id = RouteParameter。可选} ); } } 在`RouteConfig`中: 公共类RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute({resource} .axd / {* pathInfo}); routes.MapMvcAttributeRoutes(); routes.MapRoute( name:Default, url:{controller} / {action} / {id}, defaults:new {controller =Home ,action =Index,id = UrlParameter.Optional}); } } 我有这些。我检查了NuGet包中是否有AttributeRouting(ASP.NET MVC)和AttributeRouting(ASP.NET Web API),并且两者都没有安装,所以我只尝试使用AttributeRouting(ASP.NET MVC),但它给我带来了同样的错误。 scope.Department = function () { window.location.href = appSetting.publish + "/appsettings/Departments"; } and the controller to this: [RoutePrefix("appsettings")] public class SettingsController : Controller { [Route("Departments")] public ActionResult Departments() { return View(); } } It goes to Http error 404 not found page. I have referred this article(www.c-sharpcorner/UploadFile/b1df45/web-api-route-and-route-prefix-part-2/). Also in global.asax class and added the following line of code: GlobalConfiguration.Configure(WebApiConfig.Register); And in `WebApiConfig`: public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Attribute routing. config.MapHttpAttributeRoutes(); // Convention-based routing. config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } } And in `RouteConfig`: public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapMvcAttributeRoutes(); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }); } } I have all these. I checked if I have AttributeRouting (ASP.NET MVC) alongside AttributeRouting (ASP.NET Web API) in NuGet package and both were not installed so I just tried with AttributeRouting (ASP.NET MVC) but it got me the same error.

将HttpGet添加到操作方法。 Add HttpGet to action method. [HttpGet] [Route("Departments")] public ActionResult Departments() { return View(); }

更多推荐

MVC5中的Attributerouting无法正常工作

本文发布于:2023-10-21 17:45:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1514926.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:无法正常   工作   Attributerouting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!