如何在MVC中设置控制器的路由。始终返回“HTTP错误404.0 - 未找到”

时间:2013-08-07 02:10:21

标签: asp.net-mvc asp.net-mvc-4 .net-4.0 asp.net-mvc-routing

我正在尝试在Global.asax中为以下控制器设置路由,但我总是得到 HTTP错误404.0 - 未找到

AssetsController.cs

public class AssetsController : Controller
{
  public ActionResult Index(string aDesc, string fDate)
  {
    ViewBag.DateFrom = fDate.ToString();
    ViewBag.AssetDescription = aDesc.ToString();
    return View();
  }
 }

的Global.asax.cs

public class MvcApplication : System.Web.HttpApplication
{

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapRoute("Assets", "Assets/{action}/{aDesc}/{fDate}",
            new { controller = "Assets", action = "Index", aDesc = "", fDate = "" }
        );

        // Show a 404 error page for anything else.
        routes.MapRoute("Error", "{*url}",
            new { controller = "Error", action = "404" }
        );
    }

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AuthConfig.RegisterAuth();

    }
}

当我使用以下URL访问视图时,一切正常并且符合预期:

http://localhost/Assets/Index/?aDesc=090&fDate=20130701000000

但是当我使用以下URL访问视图时,出现404.0错误消息(错误如下所示)

http://localhost/Assets/Index/090/20130701000000

错误:HTTP错误404.0 - 未找到您正在寻找的资源 删除,更改名称或暂时无法使用。

AppPool设置为.NET 4.0集成模式

在“常见HTTP功能”(Windows功能开/关)下检查HTTP重定向和静态内容

1 个答案:

答案 0 :(得分:1)

我不确定您的路线是否超出默认路线,因为您明确提供参数。尝试在RouteConfig.cs

中注册您的路线

http://www.jondavis.net/techblog/post/2012/06/23/ASPNET-MVC-4-Where-Have-All-The-Globalasax-Routes-Gone.aspx