2

当我将 MVC4 应用程序发布到我的 Web 服务器时,我遇到了一个奇怪的问题。

一切正常,我确实更改了路由配置中的一些设置。

问题是当我在 localhost 上运行网站时,我的所有链接都呈现得非常好。

@Html.ActionLink("Market Summary","Market","Home") 将呈现为 url。

然而,当我将我的项目上传到我的服务器(winhost)时,每当我将鼠标悬停在我的链接上时,它只会指向我的域名。它在我项目的任何页面上执行此操作...

我不知道为什么会发生这种情况,因为它在 localhost 上运行良好。

这是我的路由配置。

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

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

        routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );


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

         );


    }
  }
 }

更新:链接实际上呈现为空白。 <a href="">

更新 2:如果我从控制器中删除 param2 和 param2,一切都会按预期工作......这到底是什么!?

更新 3:问题已解决,确实是非常奇怪的问题。看我的回答。

4

2 回答 2

2

这是 ASP.NET MVC 的一个已知问题。

我四处寻找,发现如果您使用Html.ActionLink服务器通常会尝试填写任何缺少的参数的属性,这由于某种原因导致 url 呈现为空白。

我不确定这是什么原因,我相信其他人可以提供更多的见解。

基本上,除非每个 URL 都包含参数,否则默认路由中不能有多个 ID。

所以我所做的是创建了一条 SECOND 路线并制作了如下所示的链接:

   @Html.RouteLink(Model.MembershipCount.ToString(), "detailed", new { action="YourActionName", id = "blah", param2 = "foo", param3 = "foo2" }, null) 
   //detailed is where you put whatever you named your route.

我的路由类现在看起来像这样

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

        routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

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

        );

        routes.MapRoute(
            name: "Detailed",
            url: "{controller}/{action}/{id}/{param2}/{param3}",
            defaults: new { controller = "Home", action = "home", id = UrlParameter.Optional, param2 = UrlParameter.Optional, param3 = UrlParameter.Optional }

         );             


    }
}

这工作得很好,但我仍然完全不知道为什么我的开发服务器上没有出现这个问题。也许微软已经修补了这种疯狂,而我的主机只是没有更新他们的框架。

我希望这可以节省一些人的挫败感。

于 2012-11-14T08:19:53.180 回答
1

我在 ASP .NET MVC 3 应用程序中遇到了类似的问题,并通过添加解决了

<modules runAllManagedModulesForAllRequests="true" />

进入<system.webServer>web.config 文件的部分。该应用程序在开发机器上运行良好,但在服务器上运行良好。

于 2012-11-14T07:16:30.757 回答