Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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. 
Posted
Updated 9-Mar-18 3:34am

1 solution

Add HttpGet to action method.

[HttpGet]
[Route("Departments")]
public ActionResult Departments()
{
    return View();
}
 
Share this answer
 
Comments
Member 13512111 9-Mar-18 14:20pm    
I tried but it didnt work

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900