简单路由到另一个控制器中的操作
Simple Route to an action in another controller
我有一个 MVC .Net C# 项目。在家庭控制器下有计划行动。
但我不想以 http://....../Home/Plans 的身份访问此页面
但我想以 http://....../Plans 的形式访问它
但我不想创建计划控制器。所以我不想做 redirectToAction。
我正在尝试按如下方式使用 Route Annonation:
[Route("plans/")]
public ActionResult Plans()
[Route("plans/{actions}")]
public ActionResult Plans()
[Route("plans/index")]
public ActionResult Plans()
但是上面的 none 对我有用。你们能帮帮我吗?
更新:
这是我在 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);
}
这是我的配置
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 }
);
}
首先记得配置属性路由:
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 }
);
}
然后注意每个控制器函数必须有不同的名称。在示例中,它们具有相同的名称,编译器不接受。
但同样重要的是,路由属性中的 {actions} 参数有什么用?当您定义一个属性路由时,您不需要定义一个动作,因为您的属性已经在装饰一个动作/方法。您可以在路由中包含必需/可选参数,但它们通常对应于方法签名中的匹配参数:
//Example http://www.domain.com/plans/123
[Route("plans/{productId}")]
public ActionResult Plans(string productId)
{
...
}
我有一个 MVC .Net C# 项目。在家庭控制器下有计划行动。 但我不想以 http://....../Home/Plans 的身份访问此页面 但我想以 http://....../Plans 的形式访问它 但我不想创建计划控制器。所以我不想做 redirectToAction。
我正在尝试按如下方式使用 Route Annonation:
[Route("plans/")]
public ActionResult Plans()
[Route("plans/{actions}")]
public ActionResult Plans()
[Route("plans/index")]
public ActionResult Plans()
但是上面的 none 对我有用。你们能帮帮我吗?
更新:
这是我在 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);
}
这是我的配置
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 }
);
}
首先记得配置属性路由:
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 }
);
}
然后注意每个控制器函数必须有不同的名称。在示例中,它们具有相同的名称,编译器不接受。
但同样重要的是,路由属性中的 {actions} 参数有什么用?当您定义一个属性路由时,您不需要定义一个动作,因为您的属性已经在装饰一个动作/方法。您可以在路由中包含必需/可选参数,但它们通常对应于方法签名中的匹配参数:
//Example http://www.domain.com/plans/123
[Route("plans/{productId}")]
public ActionResult Plans(string productId)
{
...
}