在默认规则之后创建直通路由规则
Creating a Fall-Through Route Rule after the Default Rule
如何在映射到产品控制器的默认路由之后为产品创建直通(回退)路由?
而不是例子。com/product/laptop我想要例子。com/laptop
/product 是一个可以完成各种工作的应用程序。但是,产品名称是动态的,并且一直在添加新名称。
如果路由存在,则应使用默认值:
example.com/about/
example.com/about/shipping
否则为产品,应落入最后一条路由规则:
example.com/{dynamic product name fallback}
example.com/laptop
example.com/mouse
example.com/iphone
我已经尝试了所有回退,但它从未到达产品控制器,也没有传递我需要的产品名称。
url: "{*.}"
路由配置:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "About", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Fall Through to Product",
url: "{productname}/",
defaults: new { controller = "Product", action = "Index" }
);
控制器
public class ProductController : Controller
{
public ActionResult Index(string productname)
{
return View();
}
}
你真的不能。默认路由是默认路由,因为它捕获了几乎所有内容。但是,您可以先尝试找到匹配的产品来处理 404。
在您的 Web.config 中,添加以下内容:
<httpErrors errorMode="Custom" existingResponse="Auto">
<remove statusCode="404" />
<error statusCode="404" responseMode="ExecuteURL" path="/error/notfound" />
</httpErrors>
然后创建ErrorController
:
public ActionResult NotFound()
{
// Get the requested URI
var uri = new Uri(Regex.Replace(Request.Url.OriginalString, @"^(.*?);(.*)", ""));
// You slug will be the `AbsolutePath`
var slug = uri.AbsolutePath.Trim('/');
// Attempt to find product
var product = db.Products.SingleOrDefault(m => m.Slug == slug);
// If no product, return 404 error page
if (product == null)
{
return View("~/Views/404.cshtml");
}
// Otherwise, return product view
Response.StatusCode = 200;
return View("~/Views/Product/Product.cshtml", product);
}
如何在映射到产品控制器的默认路由之后为产品创建直通(回退)路由?
而不是例子。com/product/laptop我想要例子。com/laptop
/product 是一个可以完成各种工作的应用程序。但是,产品名称是动态的,并且一直在添加新名称。
如果路由存在,则应使用默认值:
example.com/about/
example.com/about/shipping
否则为产品,应落入最后一条路由规则:
example.com/{dynamic product name fallback}
example.com/laptop
example.com/mouse
example.com/iphone
我已经尝试了所有回退,但它从未到达产品控制器,也没有传递我需要的产品名称。
url: "{*.}"
路由配置:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "About", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Fall Through to Product",
url: "{productname}/",
defaults: new { controller = "Product", action = "Index" }
);
控制器
public class ProductController : Controller
{
public ActionResult Index(string productname)
{
return View();
}
}
你真的不能。默认路由是默认路由,因为它捕获了几乎所有内容。但是,您可以先尝试找到匹配的产品来处理 404。
在您的 Web.config 中,添加以下内容:
<httpErrors errorMode="Custom" existingResponse="Auto">
<remove statusCode="404" />
<error statusCode="404" responseMode="ExecuteURL" path="/error/notfound" />
</httpErrors>
然后创建ErrorController
:
public ActionResult NotFound()
{
// Get the requested URI
var uri = new Uri(Regex.Replace(Request.Url.OriginalString, @"^(.*?);(.*)", ""));
// You slug will be the `AbsolutePath`
var slug = uri.AbsolutePath.Trim('/');
// Attempt to find product
var product = db.Products.SingleOrDefault(m => m.Slug == slug);
// If no product, return 404 error page
if (product == null)
{
return View("~/Views/404.cshtml");
}
// Otherwise, return product view
Response.StatusCode = 200;
return View("~/Views/Product/Product.cshtml", product);
}