i have an MVC .Net C# project. have Plan Action under Home Controller. but i dont want to access this page as http://....../Home/Plans but i want to access it as http://....../Plans but i dont want to create Plans Controller. so i dont want to do a redirectToAction.
我有一个MVC .Net C#项目。在Home Controller下有计划行动。但我不想访问此页面为http://....../Home/Plans但我想以http://....../Plans访问它,但我不想创建计划控制器。所以我不想做redirectToAction。
i am trying to use the Route Annonation as the following:
我正在尝试使用Route Annonation,如下所示:
[Route("plans/")]
public ActionResult Plans()
[Route("plans/{actions}")]
public ActionResult Plans()
[Route("plans/index")]
public ActionResult Plans()
but none of the above worked for me. can you guys help me in this.
但以上都没有为我工作。你能帮助我吗?
Updated:
this is my action under HomeController
这是我在HomeController下的动作
[Route("plans")]
public ActionResult Plans()
{
var servicePlansDto = SubscriberApiManager.SubscriptionSellingService.GetServicePlans(ServiceId).FindAll(sp => !sp.HasPromotionCode);
List<ServicePlanVm> servicePlansVm = Mapper.Map<List<ServicePlanDto>, List<ServicePlanVm>>(servicePlansDto);
return View(servicePlansVm);
}
and this is my configurations
这是我的配置
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 }
);
}
1 个解决方案
#1
First of all remember to configure attribute routing:
首先记得配置属性路由:
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 }
);
}
Then take care that each controller function has to have a different name. In the example they have the same name and this is not accepted by the compiler.
然后注意每个控制器功能必须具有不同的名称。在示例中,它们具有相同的名称,编译器不接受此名称。
Lat but not least, what's the need of the {actions} parameter in the routing attribute? When you define an attribute routing you don't need to define an action, as you your attribute is already decorating an action / method. You can have required / optional parameters in your routing but they usually correspond to a matching parameter in the method's sugnature:
Lat但并非最不重要的是,路由属性中{actions}参数的需求是什么?定义属性路由时,您不需要定义操作,因为您的属性已经在装饰操作/方法。您可以在路由中包含必需/可选参数,但它们通常对应于方法的sugnature中的匹配参数:
//Example http://www.domain.com/plans/123
[Route("plans/{productId}")]
public ActionResult Plans(string productId)
{
...
}
#1
First of all remember to configure attribute routing:
首先记得配置属性路由:
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 }
);
}
Then take care that each controller function has to have a different name. In the example they have the same name and this is not accepted by the compiler.
然后注意每个控制器功能必须具有不同的名称。在示例中,它们具有相同的名称,编译器不接受此名称。
Lat but not least, what's the need of the {actions} parameter in the routing attribute? When you define an attribute routing you don't need to define an action, as you your attribute is already decorating an action / method. You can have required / optional parameters in your routing but they usually correspond to a matching parameter in the method's sugnature:
Lat但并非最不重要的是,路由属性中{actions}参数的需求是什么?定义属性路由时,您不需要定义操作,因为您的属性已经在装饰操作/方法。您可以在路由中包含必需/可选参数,但它们通常对应于方法的sugnature中的匹配参数:
//Example http://www.domain.com/plans/123
[Route("plans/{productId}")]
public ActionResult Plans(string productId)
{
...
}