MVC5中的Attributerouting无法正常工作 [英] Attributerouting in MVC5 not working

查看:75
本文介绍了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(http://www.c-sharpcorner.com/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找不到页面。
我已经推荐过这篇文章(http://www.c-sharpcorner.com/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(http://www.c-sharpcorner.com/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无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆