作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个只有这条路线的网站:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("Default", "{controller}/{action}/{id}",
new { controller = "Image", action = "Image", id = UrlParameter.Optional }
);
}
这是 Controller :
public class ImageController : Controller
{
public ActionResult Image(int? id)
{
if (id == null)
{
// Do something
return View(model);
}
else
{
// Do something else
return View(model);
}
}
}
现在这是默认操作,因此我无需 ID 即可直接访问我的域来访问它。为了调用 id,通过/Image/Image/ID 就可以正常工作。然而我想要的是在没有图像/图像的情况下调用它(所以/ID)。现在这不起作用。
这是默认路由的限制还是有办法让它发挥作用?
谢谢
最佳答案
创建特定于此网址的新路由:
routes.MapRoute(
name: "Image Details",
url: "Image/{id}",
defaults: new { controller = "Image", action = "Image" },
constraints: new { id = @"\d+" });
确保在此之前注册上述路由:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
否则它将不起作用,因为默认路由将优先。
这里我声明,如果 url 包含“/Image/1”,则执行 ImageController/Image
操作方法。
public ActionResult Image(int id) {//.....//}
该约束意味着 {id} 参数必须是数字(基于正则表达式 \d+
),因此不需要 nullable int,除非您确实想要 nullable int,在这种情况下,请删除约束。
关于asp.net-mvc - 默认操作的可选 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26948361/
我正在尝试用 Swift 编写这段 JavaScript 代码:k_combinations 到目前为止,我在 Swift 中有这个: import Foundation import Cocoa e
我是一名优秀的程序员,十分优秀!