문제

내가 사용하려는 노선 제약 조건 Asp.Net MVC 응용 프로그램입니다.

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

할 때 내가 입력하는 url 에 다음과 같~/MyAction/문자열로,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