0

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.

1 Answer 1

2

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 }
    );
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.