C# 避免在mvc3中使用带字符串参数的索引表单Url
我有一个这样的控制动作C# 避免在mvc3中使用带字符串参数的索引表单Url,c#,asp.net-mvc-3,asp.net-mvc-routing,C#,Asp.net Mvc 3,Asp.net Mvc Routing,我有一个这样的控制动作 [HttpGet] public ActionResult Index(string Id) { } 所以实际调用类似于Report/Index/{string\u param\u value} 我想避免从这个like报告/{string_param_value}中生成索引,并以此为基础 我在Global.asax.cs中做了如下更改 routes.MapRoute( "Report_WithoutIndex",
[HttpGet]
public ActionResult Index(string Id)
{
}
所以实际调用类似于Report/Index/{string\u param\u value}
我想避免从这个like报告/{string_param_value}中生成索引,并以此为基础
我在Global.asax.cs中做了如下更改
routes.MapRoute(
"Report_WithoutIndex",
"Report/{Id}",
new { controller = "Report", action = "Index" }
);
但这并不是索引操作
我当时试过这个
routes.MapRoute(
name: "Index",
url: "{controller}/{id}",
defaults: new { action = "Index" },
constraints: new { action = "Index" }
);
这一个对我有效,但在这之后,所有其他动作都被破坏了
那么,在调用Report controller Wu时,正确的解决方法是什么呢?在
RounteConfig.cs
之后,不提及url中的索引
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Report_WithoutIndex",
"Report/{Id}",
new { controller = "Report", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
我的控制器是-
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your app description page.";
return View();
}
}
public class ReportController : Controller
{
public ActionResult Index(string Id)
{
return null;
}
}
当我使用
/Report/2
时,我正在点击报表控制器索引操作。当我使用/Home/About
时,我要转到关于家庭控制器的操作。所有其他默认路由都按预期工作。发布整个RounteConfig.cs
,确保第一条路由在默认路由之前。是的,这就是问题所在。我必须将我的自定义路线作为默认路线规则之前的第一行。您的Id
是否始终为数字?请检查我的答案。