簡體   English   中英

如何為特定路由Asp.Net MVC構建我的操作方法

[英]How do I construct my action method for a specific route Asp.Net MVC

我有兩條路線要嘗試使用,例如

www.mysite.com/Rate/Student/Event/123

www.mysite.com/Rate/Teacher/Event/1234

routes.MapRoute(
    name: "Rate",
    url: "Rate/Student/Event/{id}"
);

routes.MapRoute(
    name: "Rate",
    url: "Rate/Teacher/Event/{id}"
);

如何構造動作方法?

這是我的費率控制器中的內容

public ActionResult Student(int id)
{
    return View();
}

public ActionResult Teacher(int id)
{
    return View();
}

您已經設置了路由以匹配URL,但是尚未告訴MVC將請求發送到哪里。 MapRoute通過使用路由值來工作 ,該路由值可以默認為特定值,也可以通過URL傳遞。 但是,您都不做。

注意: MVC中需要controlleraction路線值。

選項1:添加默認路由值。

    routes.MapRoute(
        name: "Rate",
        url: "Rate/Student/Event/{id}",
        defaults: new { controller = "Rate", action = "Student" }
    );

    routes.MapRoute(
        name: "Rate",
        url: "Rate/Teacher/Event/{id}",
        defaults: new { controller = "Rate", action = "Teacher" }
    );

選項2:通過URL傳遞路由值。

    routes.MapRoute(
        name: "Rate",
        url: "{controller}/{action}/Event/{id}"
    );

暫無
暫無

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

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