Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.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/2/.net/21.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# 如何将请求路由到MVC2中特定控制器中的特定操作?_C#_.net_Asp.net_Asp.net Mvc_Iis - Fatal编程技术网

C# 如何将请求路由到MVC2中特定控制器中的特定操作?

C# 如何将请求路由到MVC2中特定控制器中的特定操作?,c#,.net,asp.net,asp.net-mvc,iis,C#,.net,Asp.net,Asp.net Mvc,Iis,在我的MVC2应用程序中,我有一个继承自Controller的AccountController类。我想实现以下功能:当用户尝试打开帐户/付款/NewPayment帐户控制器时,应调用executeewpayment()方法 我添加了以下路线: routes.MapRoute( @"CreateNewRuntime", @"{langId}/Account/Payments/NewPayment/{whatever}", new { langId = @"default"

在我的MVC2应用程序中,我有一个继承自
Controller
AccountController
类。我想实现以下功能:当用户尝试打开
帐户/付款/NewPayment
帐户控制器时,应调用executeewpayment()
方法

我添加了以下路线:

routes.MapRoute(
    @"CreateNewRuntime",
    @"{langId}/Account/Payments/NewPayment/{whatever}",
    new { langId = @"default", controller = @"Account", action = @"ExecuteNewPayment"});
但是当我尝试请求上面的路径时,我会收到带有“Requested URL”
/Account/Payments/NewPayment
的HTTP 404错误消息,当我在调试器下执行此操作时,会出现异常

System.Web.Mvc.dll中发生类型为“System.Web.HttpException”的第一次意外异常 其他信息:路径“/Account/Payments/NewPayment”的控制器未找到或未实现IController


我做错了什么?如何执行映射?

{language}
没有对应的默认值,
langid
应该是
language=@“默认值”

{language}
没有对应的默认值,是
langid
应该是
language=@“默认值”

您需要的是自定义路由处理程序 看看


注意。

您需要的是一个自定义路由处理程序 看看


注意。

您需要将
langid
作为路由的一部分,否则即使您指定了默认值,MVC也不会将您的URI映射到它

采取以下两条路线:

routes.MapRoute(
    @"CreateNewRuntime",
    @"{langId}/Account/Payments/NewPayment/{whatever}",
    new { langId = @"default", controller = @"Account", action = @"ExecuteNewPayment"});


routes.MapRoute(
    @"CreateNewRuntime1",
    @"{langId}/{subLangId}/Account/Payments/NewPayment/{whatever}",
    new { langId = @"default", subLangId= @"test", controller = @"Account", action = @"ExecuteNewPayment1"});
如果我指定
/Account/Payments/NewPayment
的URI,它应该选择哪一条路由?如果MVC确实使用了langId的默认值,那么第一条路由将始终被使用,因为它是在另一条路由之前声明的。如果您交换了这两个,那么第二个将始终被调用


当URI开头有不同的数据时,需要指定它们,并且在指定路由时不能使用默认值。为了使这两个路由成功,您需要一个URI
/eng/Account/Payments/NewPayment
/eng/e/Account/Payments/NewPayment
您需要将
langid
作为路由的一部分,否则即使您指定了默认值,MVC也不会将您的URI映射到它

采取以下两条路线:

routes.MapRoute(
    @"CreateNewRuntime",
    @"{langId}/Account/Payments/NewPayment/{whatever}",
    new { langId = @"default", controller = @"Account", action = @"ExecuteNewPayment"});


routes.MapRoute(
    @"CreateNewRuntime1",
    @"{langId}/{subLangId}/Account/Payments/NewPayment/{whatever}",
    new { langId = @"default", subLangId= @"test", controller = @"Account", action = @"ExecuteNewPayment1"});
如果我指定
/Account/Payments/NewPayment
的URI,它应该选择哪一条路由?如果MVC确实使用了langId的默认值,那么第一条路由将始终被使用,因为它是在另一条路由之前声明的。如果您交换了这两个,那么第二个将始终被调用


当URI开头有不同的数据时,需要指定它们,并且在指定路由时不能使用默认值。为了让这两条路线被点击,您需要一个URI
/eng/Account/Payments/NewPayment
/eng/e/Account/Payments/NewPayment
,我相信您应该使用这些路线

routes.MapRoute(
                @"CreateNewRuntime1",
                @"Account/Payments/NewPayment/{whatever}",
                new { langId="en-US",controller = @"Account", action = @"ExecuteNewPayment" });

        routes.MapRoute(
                @"CreateNewRuntime2",
                @"{langId}/Account/Payments/NewPayment/{whatever}",
            new { controller = @"Account", action = @"ExecuteNewPayment" },
            new { langId = "[a-z]{2}-[a-z]{2}" });

请注意,在第一个路由中,我没有使用langId并将langId默认值设置为“en-US”。在第二个路由中,langId是必需的,但是有一个正则表达式,这样就不会干扰页面的其他路由。没有这个正则表达式,langid可以是其他任何东西。

我相信你应该使用这些方法

routes.MapRoute(
                @"CreateNewRuntime1",
                @"Account/Payments/NewPayment/{whatever}",
                new { langId="en-US",controller = @"Account", action = @"ExecuteNewPayment" });

        routes.MapRoute(
                @"CreateNewRuntime2",
                @"{langId}/Account/Payments/NewPayment/{whatever}",
            new { controller = @"Account", action = @"ExecuteNewPayment" },
            new { langId = "[a-z]{2}-[a-z]{2}" });

请注意,在第一个路由中,我没有使用langId并将langId默认值设置为“en-US”。在第二个路由中,langId是必需的,但是有一个正则表达式,这样就不会干扰页面的其他路由。没有这个正则表达式,langid可以是其他任何东西。

我更改了它,但它没有任何帮助。我也有很多其他的路线与这种不匹配-他们的工作。我改变了,但它没有帮助。我也有很多其他的路线与这样的不匹配-他们的工作。这是可悲的。那么,我是否必须指定一个额外的路由,以便
/language/Account/Payments/NewPayment
/Account/Payments/NewPayment
映射到同一个控制器操作对,还是有更好的方法?@sharptooth-不幸的是,对于这两种场景,您都需要一个路由。那么,我是否必须指定一个额外的路由,以便
/language/Account/Payments/NewPayment
/Account/Payments/NewPayment
映射到同一个控制器操作对,还是有更好的方法?@sharptooth-不幸的是,这两个场景都需要路由