簡體   English   中英

C#MVC-多控制器中的名稱空間

[英]C# MVC - namespace in multi controller

我想在SomeController中使用AnotherController名稱。 但是,RoutePrefix屬性只能由控制器級別聲明。

准備以下內容。

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

它沒有按我想要的那樣工作。

我知道我可以創建一個新的控制器並執行此操作,但是我不想這么做。

我想從熟悉Route的您那里得到一些建議。

您需要添加routes.MapMvcAttributeRoutes(); 在默認路由寄存器之前,

您的RegisterRoutes函數應該像

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM