Reputation: 13
I want use AnotherController name in SomeController. But, RoutePrefix Attribute is can only be declared by Controller level.
Prepare the following.
namespace KRSMART.Controllers
{
public class SomeController : Controller
{
/* localhost:0000/Some/Index */
public ActionResult Index()
{
return View();
}
/* I want Url */
/* localhost:0000/Another/Test */
[Route("Another/Index")]
public ActionResult Test()
{
return View();
}
}
}
It didn't work as I wanted it to.
I know I can create a new controller and do it, but I didn't want to.
I'd like to get some advice from you who are familiar with Route.
Upvotes: 0
Views: 434
Reputation: 4298
You need to add routes.MapMvcAttributeRoutes();
before default route register,
your RegisterRoutes
function should be like,
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 }
);
}
Upvotes: 2