Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我可以有一个通用参数名的路由吗_C#_Asp.net Mvc_Asp.net Mvc 5_Asp.net Mvc Routing - Fatal编程技术网

C# 我可以有一个通用参数名的路由吗

C# 我可以有一个通用参数名的路由吗,c#,asp.net-mvc,asp.net-mvc-5,asp.net-mvc-routing,C#,Asp.net Mvc,Asp.net Mvc 5,Asp.net Mvc Routing,我正在研究ASP.NETMVC路由。我想创建一个将参数传递到操作的路由,而不考虑参数名称。我已经读了一些关于这方面的书,并尝试了不同的选择,但无法让它工作,所以想知道这是否可行 下面是我的问题和一个例子: 在我的StudentController中,我有两种动作方法 // First Action in StudentController public ActionResult GetStudentByName(string name) // Second Action in StudentC

我正在研究ASP.NETMVC路由。我想创建一个将参数传递到操作的路由,而不考虑参数名称。我已经读了一些关于这方面的书,并尝试了不同的选择,但无法让它工作,所以想知道这是否可行

下面是我的问题和一个例子:

在我的StudentController中,我有两种动作方法

// First Action in StudentController
public ActionResult GetStudentByName(string name)

// Second Action in StudentController
public ActionResult GetStudentById(int id)
现在,在我的路线配置中,我有:

// Get Student By Name
routes.MapRoute(
   name: "StudentByName",
   url: "Student/GetStudentByName/{name}",
   defaults: new { controller = "Student", action = "GetStudentByName", name = "" }
);

// Get Student By Id
routes.MapRoute(
    name: "StudentById",
    url: "Student/GetStudentById/{id}",
    defaults: new { controller = "Student", action = "GetStudentById", id = UrlParameter.Optional }
    );
这很好,但我必须为这两个操作定义两条路径。我的操作需要具有不同名称(名称和id)的参数

我想要一个通用路由,它处理Action方法并将参数传递给Action,类似这样的

// Is this possible?
routes.MapRoute(
    name: "AspDefault",
    url: "{controller}/{action}/{GenericParamName}",
    defaults: new { controller = "Home", action = "Index", GenericParamName = UrlParameter.Optional }
);
我已经试过了,但无法使它工作。如果动作中的参数名和路由不匹配,则它们似乎无法通过

是否可以用一条路径处理这两种操作方法?如果是,怎么做?

是否可以用一条路径处理这两种操作方法?如果是,怎么做

您需要将两个操作上的参数命名为相同的名称,以匹配路由

比如说

//Student/GetStudentByName/JDoe
//Student/GetStudentById/13456
routes.MapRoute(
    name: "StudentDefault",
    url: "Student/{action}/{value}",
    defaults: new { controller = "Student", value = UrlParameter.Optional }
);
上述路线意味着控制器操作必须更新为

public class StudentController : Controller {

    // First Action in StudentController
    public ActionResult GetStudentByName(string value) {
        //...
    }

    // Second Action in StudentController
    public ActionResult GetStudentById(int value) {
        //...
    }

}