简体   繁体   中英

C# MVC - namespace in multi controller

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.

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 }
    );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM