Member 13512111 Ответов: 1

Attributerouting в MVC5 не работает


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


Что я уже пробовал:

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. 

1 Ответов

Рейтинг:
2

Nagaraj Muthuchamy

Добавьте HttpGet в метод действия.

[HttpGet]
[Route("Departments")]
public ActionResult Departments()
{
    return View();
}


Member 13512111

Я пытался, но это не сработало