asp.net mvc:我们可以在路由不匹配时设置http状态代码

时间:2017-12-05 11:54:15

标签: asp.net-mvc-4 asp.net-mvc-5

假设这是我的路线,它期望productid的整数数据

routes.MapRoute(
    "Product",
    "Product/{productId}",
    new {controller="Product", action="Details"},
    new {productId = @"\d+" }
 );

所以这些路线会起作用

/Product/3
/Product/8999 

我怎样才能将用户重定向到预先存在的视图,我可以在这个视图中显示此方案的友好错误消息?

我想知道的另外一件事我们可以在用户输入/Product/apple时设置状态代码,并且此路由将不匹配。所以在这种情况下我想设置statusCode="404"。如果我可以这样做,那么在我指定视图的customErrors下面将显示404错误。建议我怎么做?

<customErrors mode="Off" defaultRedirect="~/error.html">
  <error statusCode="403" redirect="~/access-denied.aspx" />
  <error statusCode="404" redirect="~/page-not-found.aspx" />
  <error statusCode="500" redirect="~/error.html" />
</customErrors>
如果有可能,请指导我。

1 个答案:

答案 0 :(得分:0)

您当前列出的路线与您说的请求不符。

您应该考虑添加两条这样的路线。

[[[ALAssetsLibrary alloc] init] writeVideoAtPathToSavedPhotosAlbum:outputFileURL completionBlock:^(NSURL *assetURL, NSError *error) {
        if (error)
            NSLog(@"%@", error);

        [[NSFileManager defaultManager] removeItemAtURL:outputFileURL error:nil];

    }];

现在,当 routes.MapRoute( "Product", "Product/{productId}", new { controller = "Product", action = "Details" }, new { productId = @"\d+" } ); routes.MapRoute( "ProductNotMatched", "Product/{productId}", new { controller = "Product", action = "NotFound" }); 被点击时,系统会调用NotFound

接下来,您可以创建一个操作并将其重定向,就像这样,

/Product/apple

希望有所帮助