我想在一个Asp.Net MVC应用程序使用路由约束。

routes.MapRoute(
    "theRoute",
    "MyAction/{page}",
    new { controller = "TheController", action = "MyAction", page = 1 },
    new { page = @"[0-9]" });

当我输入像〜/ MyAction / ASTRING一个URL,一个YSOD被示出具有无效操作异常。我能做些什么,以无效的URL重定向到404页?

我知道可以解决这个问题与控制器操作和int.TryParse字符串参数,但随后的路线constaint是无用的。

如何选择是由路由约束抛出的exceptiontype?

有帮助吗?

解决方案

问题是,你不必是在一个字符串结束路由匹配的路由。

修改您的路由类似于:

routes.MapRoute(
    "Default",                                              // Route name
    "{controller}/{action}/{id}",                           // URL with parameters
    new { controller = "Home", action = "Index", id = 0 },
    new { id = "[0-9]" }// Parameter defaults
);
routes.MapRoute(
    "Default2",                                              // Route name
    "{controller}/{action2}/{sid}",                           // URL with parameters
    new { controller = "Home", action = "Index2", sid = "" }  // Parameter defaults
);

和修改控制器

public ActionResult Index(int id)
    {
        ViewData["Title"] = "Home Page";
        ViewData["Message"] = "Welcome to ASP.NET MVC! Your id is: "+ id.ToString();

        return View();
    }

    public ActionResult Index2(string sid)
    {
        ViewData["Title"] = "Home Page 2."+sid.ToString();
        ViewData["Message"] = "Welcome to ASP.NET MVC! \"" + sid.ToString() +"\" is an invalid id";

        return View("index");
    }

现在,当你传递一个字符串的ID,索引2将被调用,你可以做任何你需要做的处理不正确的参数。

其他提示

仅举一个更一般的重定向:

您可以在应用程序的Web.config写:

<system.web>
    ...
    ...
    <customErrors mode="On">
        <error 
            statusCode="404" 
            redirect="/Home/MyCustomError" /> 
                                <!--    Is not necessary that the 
                                        view MyCustomError.aspx are inside the 
                                        Home folder, you can put that 
                                        view in the Shared folder.
                                -->
    </customErrors>
    ...
    ...
</system.web>

然后,你需要有称为ActionResult MyCustomError

public class HomeController : Controller
{
    ...
    ...

    public ActionResult MyCustomError(string aspxerrorpath) 
                                                    /* the var aspxerrorpath 
                                                     * is that MVC generated by
                                                     * default */
    {
        ViewData["messageError"] = aspxerrorpath;
        return View();
    }
}

然后你就可以自定义错误页:

<%@ Page Language="C#" 
        MasterPageFile="~/Views/Shared/Site.Master" 
        Inherits="System.Web.Mvc.ViewPage<System.Web.Mvc.HandleErrorInfo>" %>

<asp:Content ID="errorTitle" ContentPlaceHolderID="TitleContent" runat="server">
    Error
</asp:Content>

<asp:Content ID="errorContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2>Shit happends</h2>
     <p> <%: ViewData["messageError"]%></p>
     <p>aaaaaaaaaaaaaaa!!!!!!!!!!!!!!!!!!!!</p>
</asp:Content>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top