Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET MVC 4-要删除的映射路由配置'/主页';其中已存在默认配置_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 4_Routes - Fatal编程技术网

C# ASP.NET MVC 4-要删除的映射路由配置'/主页';其中已存在默认配置

C# ASP.NET MVC 4-要删除的映射路由配置'/主页';其中已存在默认配置,c#,asp.net,asp.net-mvc,asp.net-mvc-4,routes,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Routes,我有一个解决方案,其中初始和默认(RouteConfig)页面是“登录/索引”。此页面的名称让用户理解,用于用户登录系统。成功登录后,将被重定向到主页 登录页面显示的url为,在此之后,“主页”页面中的url为Home。如何配置控制器在活动中的主页索引在链接上不显示“主页”?换言之,要平等地工作 我尝试了一些替代方案,但我仍然没有找到一个解决方案来解决这一具体情况 谢谢 public static void RegisterRoutes(RouteCollection routes)

我有一个解决方案,其中初始和默认(RouteConfig)页面是“登录/索引”。此页面的名称让用户理解,用于用户登录系统。成功登录后,将被重定向到主页

登录页面显示的url为,在此之后,“主页”页面中的url为Home。如何配置控制器在活动中的主页索引在链接上不显示“主页”?换言之,要平等地工作

我尝试了一些替代方案,但我仍然没有找到一个解决方案来解决这一具体情况

谢谢

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

        routes.LowercaseUrls = true;

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional },
            namespaces: new [] { "Solution.UI.WEB.Controllers" }
        );
    }

让我们看看我是否理解这个问题

您只有一条路由“默认”

要使Url上的“Home”消失,您需要做的是在“defaults”中将控件“Login”更改为“Home”。它将是这样的:

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces: new [] { "Solution.UI.WEB.Controllers" }
            );
因此,在HomeController.cs上的操作“Index”中,您验证用户是否已登录,如果未登录,则重定向到操作(“Index”,“Login”)


希望对您有所帮助。

您可以在主视图上通过javascript
replaceState
进行尝试

<script>
    (function () {
        var path = document.URL.match(/\/\w+(?=\?)/g)[0];
        window.history.replaceState('string', 'Some Title Text', path.replace('/',''));
    })();
</script>

(功能(){
var path=document.URL.match(/\/\w+(?=\?)/g)[0];
window.history.replaceState('string','Some Title Text',path.replace('/','');
})();

对不起,你的问题是什么?成功登录后是否要重定向到/home?不能从一个url调用两个操作。您可以呼叫主控制器或登录控制器。@ssilas777重定向到“主”正在工作,但url显示“/home”。我需要删除这个“/主页”。这个问题可能已经被弄糊涂了,因为我解释了整个情况。@UbiquitousDevelopers我需要更改使“Home”成为默认的“url”,如果用户没有会话,则通过代码重定向到登录?这是一个遗留系统,以一种有点奇怪的方式构建。如果您想将主控制器作为默认控制器,请将控制器更改为“主”。在主页的索引操作上,您可以检查会话。如果会话为空,则重定向到登录控制器
<script>
    (function () {
        var path = document.URL.match(/\/\w+(?=\?)/g)[0];
        window.history.replaceState('string', 'Some Title Text', path.replace('/',''));
    })();
</script>