【问题标题】:.NET MVC URL Routing Help.NET MVC URL 路由帮助
【发布时间】:2011-08-02 16:04:18
【问题描述】:

我正在尝试在我的 .NET MVC 应用程序中配置一个 ErrorController,但我目前无法在控制器上执行任何操作,所以我相信这可能是因为我需要在 global 中注册 URL 路由.asax

错误控制器如下:

public class ErrorController: Controller
{

    /*Default Redirect Error Page*/
    public ActionResult Index()
    {
        return View();
    }
    /*Generic Error Page*/
    public ActionResult Generic()
    {
        return View();
    }
    /*Status Code: 400*/
    public ActionResult NotFound()
    {
        return View();
    }
}

我希望能够分别通过以下 URL 调用上述操作。

~/错误/
~/错误/通用
~/错误/未找到

我相信在 Global.asax 文件中,我需要使用以下内容注册这些路由:

routes.Add(new Route("error/{action}", new MvcRouteHandler())){controller = "Error", action = "";

如何为此添加/指定正确的路由处理程序?

【问题讨论】:

    标签: asp.net asp.net-mvc mvcroutehandler


    【解决方案1】:

    我相信 global.asax 中提供的默认网址适用于您的网址,但如果您想要一个特定的网址,请选择:

    routes.MapRoute(
          "Error", // Route name
          "error/{action}", // URL without parameters
           new { controller = "Error", action = "Index" }, // Parameter defaults
    );
    

    如果你想要参数:

    routes.MapRoute(
          "Error", // Route name
          "error/{action}/{param}", // URL with parameters
           new { controller = "Error", action = "Index", param = UrlParameter.Optional }, // Parameter defaults
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-31
      • 2011-08-17
      • 2015-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多