简体   繁体   English

将根请求路由到此控制器并在MVC中执行操作

[英]Routing root request to this controller and action in MVC

I have a URL request in the following format: http://localhost/activate?activationCode=X 我有以下格式的URL请求: http://localhost/activate?activationCode=X

I would like this request to be handled in the Home controller, by the Activate action. 我希望通过“ Activate操作在Home控制器中处理此请求。

I am not sure how to proceed. 我不确定如何进行。 I have looked at RouteConfig.cs and see the way routes are defined here: 我查看了RouteConfig.cs并查看了此处定义路由的方式:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

But how can I make Home/Activate handle URL's of the following format? 但是,如何使Home/Activate处理以下格式的URL? http://localhost/activate?activationCode=X

...so as to add a special case where activate proceeding the host name goes to the Home controller, and Activate action? ...以便添加一个特殊情况,即activate过程将主机名转到Home控制器,然后执行Activate操作?

You need to create a specific route 您需要创建特定的路线

routes.MapRoute(
    name: "Activate",
    url: "Activate/{id}",
    defaults: new { controller = "Home", action = "Activate", id = UrlParameter.Optional }
);

and place this one before the default route. 并将其放置在默认路由之前。

In addition if you want http://localhost/activate/X rather than http://localhost/activate?activationCode=X , then change it to 另外,如果您要使用http://localhost/activate/X而不是http://localhost/activate?activationCode=X ,则将其更改为

routes.MapRoute(
    name: "Activate",
    url: "Activate/{activationCode}",
    defaults: new { controller = "Home", action = "Activate", activationCode = UrlParameter.Optional }
);

and make the method in HomeController 并在HomeController制作方法

public ActionResult Activate(string activationCode)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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