c# mvc how to map the root url to a specific view

╄→гoц情女王★ 提交于 2019-12-10 17:09:53

问题


I have an mvc app with a DefaultController which maps to index.cshtml so

http://localhost:<port>/Default/Index

works fine. but the default root page:

http://localhost:<port>/

is not mapped to anything. How can I map it to my Default/Index?

Here is my RouteConfig.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MVC5_HttpClientTest_F45
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );


        }
    }
}

回答1:


Change your Default Map route controller to

routes.MapRoute(
   name: "Default",
   url: "{controller}/{action}/{id}",
   defaults: new { controller = "Default", action = "Index", id = UrlParameter.Optional }
);


来源:https://stackoverflow.com/questions/26132022/c-sharp-mvc-how-to-map-the-root-url-to-a-specific-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!