MVC 路由不起作用
更新:
正如有人指出的那样,我在路线注册中缺少 s。现在我有一个次要问题。
这就是我希望它工作的方式:
http://localhost/products/ --> ProductsController.Index()
http://localhost/products/3/apples --> ProductsController.Details(int? id, string ProductName)
这是当前发生的情况:
http://localhost/products 转到我的详细信息操作。
我该如何为此设置路线?
我在 Global.asx 文件中设置了以下路由:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"ViewProduct",
"products/{id}/{productName}",
new { controller = "Products", action = "Details", id = 0, productName = "" } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
我有以下控制器:
public class ProductsController : Controller
{
//
// GET: /Products/
public ActionResult Index()
{
IList<Product> products = new List<Product>
{
new Product { Id = 57349, ProductName = "Apple" },
new Product { Id = 57350, ProductName = "Banana" }
};
return View(products);
}
public ActionResult Details(int? id, string productName)
{
Product p = new Product { Id = id.Value, ProductName = productName };
return View(p);
}
}
Update:
As someone pointed out I was missing an s in my Route registration. Now I have a secondary problem.
This is how I want it to work:
http://localhost/products/ --> ProductsController.Index()
http://localhost/products/3/apples --> ProductsController.Details(int? id, string productName)
This is what currently happens:
http://localhost/products goes to my Details action.
How do I set up the routes for this?
I set up the following routes in my Global.asx file:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"ViewProduct",
"products/{id}/{productName}",
new { controller = "Products", action = "Details", id = 0, productName = "" } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
I have the following controller:
public class ProductsController : Controller
{
//
// GET: /Products/
public ActionResult Index()
{
IList<Product> products = new List<Product>
{
new Product { Id = 57349, ProductName = "Apple" },
new Product { Id = 57350, ProductName = "Banana" }
};
return View(products);
}
public ActionResult Details(int? id, string productName)
{
Product p = new Product { Id = id.Value, ProductName = productName };
return View(p);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的路由声明操作中缺少一个“s”
=“Details”
,或者您的操作名称中多了一个“s”:
public ActionResult Details(int ? id, string ProductName)
由您决定要纠正哪一个;)
更新:对于路线更新,只需使用:
因此,当您输入 /products 时,将使用“默认”路线。
You're missing a "s" in your route declaration
action = "Details"
Or have one exceeding "s" in your action name :
public ActionResult Details(int? id, string productName)
It's up to you to decide which one to correct ;)
Update : for the route update just use :
So when you type /products the "Default" route gets used.
因为您已经定义了
id
和productName
的默认值,所以路由引擎只会在缺少这些值时为您填充这些值,因此您对 /products 的请求将插入这些默认值,并且进入细节行动。取出:
id = 0, ProductName = ""
,您将获得预期的行为。编辑
考虑在其中添加一个 {action} 参数。由于您有默认操作并且无法覆盖它,因此您可能会将所有内容都路由到详细信息。
Because you have defined default values for
id
andproductName
the routing engine just fills those in for you when they are missing, so your request to /products is getting those defaults inserted and going to the details action.Take out:
id = 0, productName = ""
and you'll get the expected behavior.edit
Consider having an {action} parameter in there. Since you have a default action and no way to override it you may have everything routed to details anyway.