Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.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
Asp.net mvc 4 是什么导致浏览器URL行在MVC4 asp.net中被路由填充_Asp.net Mvc 4_Asp.net Routing - Fatal编程技术网

Asp.net mvc 4 是什么导致浏览器URL行在MVC4 asp.net中被路由填充

Asp.net mvc 4 是什么导致浏览器URL行在MVC4 asp.net中被路由填充,asp.net-mvc-4,asp.net-routing,Asp.net Mvc 4,Asp.net Routing,当我进入我的主页 http://localhost:5119/ 它重定向到 http://localhost:5119/Home (这就是我的浏览器url栏中显示的内容,我希望它不显示/Home) 我知道这是我的默认路线,但我不知道是什么原因导致我的URL行在浏览器中被重写。当您转到基本URL时,Visual Studio 2012中的默认示例没有此问题。我正在附上一张路由调试的图片(这似乎对我没有帮助,但可能有一些价值) 谢谢,-彼得 在后面加上这个。它是路线代码的相关部分

当我进入我的主页

http://localhost:5119/  
它重定向到

http://localhost:5119/Home
(这就是我的浏览器url栏中显示的内容,我希望它不显示/Home)

我知道这是我的默认路线,但我不知道是什么原因导致我的URL行在浏览器中被重写。当您转到基本URL时,Visual Studio 2012中的默认示例没有此问题。我正在附上一张路由调试的图片(这似乎对我没有帮助,但可能有一些价值)

谢谢,-彼得

在后面加上这个。它是路线代码的相关部分

        // HOME
        int currentYearInt;
        Int32.TryParse(currentYear, out currentYearInt);
        routes.MapRoute("HomeRouteAll", "Home/{yearInt}",
                  new
                  {
                      /* Your default route */
                      controller = "Home",
                      action = "Index"
                  });



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

您之所以看到这一点,是因为您的默认路线上方的路线
Home/{year}
具有
{year}
参数的默认值

路由引擎做出以下决定:

  • 从路线列表的顶部开始
  • 查找与路由值匹配的内容(controller=“Home”、Action=“Index”)
  • 在第一次匹配时,返回,这是您的URL
  • 由于您有一个匹配的控制器(Home)和操作(Index)以及year参数的默认值,因此路由引擎与路由
    Home/{year}
    匹配,从而给出url
    http://domain.com/Home

    快速修复方法是:a)使年份没有默认值(
    Home/2013
    ),b)将所有的内容移动到不同的控制器(
    NewName/{year}
    ),c)将其移动到不同的操作(
    NewIndex/{year}
    ),或者d)更新默认路由以使用year参数而不是id

    routes.MapRoute(
                    "Default",
                    "{controller}/{year}/{action}",
                    new {controller = "Home", action = "Index", year = 2013});
    
    编辑

    我不确定你的路线定义中有什么关于tryParse的东西,但在我的测试中,这似乎完成了你想要做的事情:

    routes.MapRoute(
                    name: "Test",
                    url: "Home/{year}",
                    defaults: new { controller = "Home", action = "Index"}, 
    //this line causes the year to be an integer
    constraints: new { year = @"\d+" }
                    );
    
                routes.MapRoute(
                    name: "Default",
                    url: "{controller}/{action}/{id}",
                    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                );
    
    行为:

    ->调用主控器,使用年份参数的空值索引操作(无重定向到主控/2013)

    ->调用主控制器,使用2010年参数索引操作

    ->呼叫总部控制器,使用该年的空值索引操作

    如果在两条路线中的第一条路线中定义了默认年份,则转到将调用主控制器,索引该年份的2013年操作,并且重定向仍不会发生

    最后,我怀疑您的主页/索引操作如下所示:

    public ActionResult Index(int year){...}
    
    如果在点击Home/Index操作时,希望自动填充2013,则将int更改为可为null的参数,并在那里执行,而不是在路由中执行

    public ActionResult Index(int? year){
        if(!year.hasValue){
            year = 2013;
        }
    

    通过在这里执行此逻辑而不是路由,您应该防止重定向到Home/因为您没有匹配的
    {year}
    参数。

    这是一种奇怪的行为。它应该转到
    /
    。我不确定这是否会有帮助,但请发布您的路线。嗨,汤米,我尝试了所有的门,最喜欢选择A。我将我的回家路线改为//home int currentYearInt;Int32.TryParse(当前年份,超出当前年份int);MapRoute(“HomeRouteAll”、“Home/{yearInt}”、新的{/*您的默认路由*/controller=“Home”、action=“Index”});但我仍然会有这样的行为,默认情况下,我会在…/homebuth,顺便说一句,我不想改变我的解除路线,因为我对其他我没有明确说明的路线都依赖于这一点。@PeterKellner你能在你的问题中发布这些内容吗?我一直在想这一点。这是正确的协议吗?我总是犹豫是否要改变我原来的问题,因为这样一来,评论和答案就没有意义了。我试试看。如果你的问题增加了讨论内容,你可以编辑它。通常,我将使用两个asterix和键入EDIT来帮助识别这是新信息