asp.net-mvc - ASP.NET MVC + WebForms - 路由冲突

标签 asp.net-mvc asp.net-mvc-routing

我正在构建一个简单的报表 Web 应用程序,用于使用 ASP.NET MVC3 + WebForms 呈现报表。报告本身由 ReportViewer ASP.NET WebForms 控件呈现,但我想使用 ASP.NET MVC 创建参数条目。

我希望所有请求都遵循“~/{controller}/{action}/{parameters}”的默认路由方案,但对 ~/Report 的请求除外。 ,这应该去呈现WebForm的报告。这样做的正确方法是什么?

有点膨胀。。

我在 Global.asax.cs 中有两条路线- WebForms 页面的默认一个和一个。

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

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );

    routes.MapPageRoute("report-rendering", "Report", "~/Render.aspx");
}

URL 渲染得很好,但问题是当请求进来时,第一个路由也吃掉第二个路由的 URL,即 ~/Report?id=7尝试调用Index ReportController 上的方法(不存在)。

如果我将其更改为“report-rendering”路由位于“Default”路由之前,如下所示:
public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapPageRoute("report-rendering", "Report", "~/Render.aspx");

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );
}

现在调用 Html.ActionLink() 呈现不正确的 URL,即
`@Html.ActionLink("Report list", "Index", "ReportList")`

渲染
`http://localhost:49910/Report?action=Index&controller=ReportList`

我当前的解决方法首先将“默认”路由放在首位,同时添加正则表达式约束以忽略对“报告” Controller 的请求,如下所示:
routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
    new { controller = @"(?!report$).*" }
);

这感觉不干净。同样,这样做的正确方法是什么?

另外,我还没有决定如何将参数传递给渲染表单:我可以使用两个查询参数或 POST 它们。我猜查询参数更灵活。这里的最佳做法是什么?

编辑:

在研究@LeftyX 的答案时,似乎我找到了答案。引用 Professional ASP.NET MVC 3 中路由章节中的 P. Haack (命名路线,第 9 章,第 233 页):

... Use names for all your routes and always use the route name when generating URLs. Most of the time, letting Routing sort out which route you want to use to generate a URL is really leaving it to chance, which is not something that sits well with the obsessive-compulsive control freak developer. When generating a URL, you generally know exactly which route you want to link to, so you might as well specify it by name.



提到的部分讨论了与我描述的情况非常相似的情况。

但是自从Html.ActionLink()路由名称参数没有重载,这是否意味着如果有这样的路由,我不能在整个应用程序的任何地方可靠地使用它?

最佳答案

这是我想出的最好的解决方案。
我已经用 MapPageRoute 注册了我的路线(我已将我的报告页面放在名为 Reports 的文件夹下)

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

    routes.MapPageRoute(
          "report-rendering",
          "Report/{id}",
          "~/Reports/Report.aspx"
         );

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
            );
}

我已经使用 RouteLink 创建了我的链接,因此您可以指定要使用的路线:
@Html.RouteLink("Report list", "report-rendering", new { id = 7 })

我可以得到编号 在我的 WebForm 页面中,如下所示:
protected void Page_Load(object sender, EventArgs e)
{
    var id = Page.RouteData.Values["id"] as string;
}

希望能帮助到你。

更新 :

我创建了一个扩展方法来让你的生活更轻松:
public static class ExtensionMethods
{
    public static MvcHtmlString WebFormActionLink(this HtmlHelper htmlHelper, string linkText, string ruoteName, object routeValues)
    {
        var helper = new UrlHelper(htmlHelper.ViewContext.RequestContext);

        var anchor = new TagBuilder("a");
        anchor.Attributes["href"] = helper.RouteUrl(routeName, routeValues);
        anchor.SetInnerText(linkText);
        return MvcHtmlString.Create(anchor.ToString());
    }
}

最好使用 行动链接 而不是 WebFormActionLink 但我的签名有问题,我不是这方面的专家。

关于asp.net-mvc - ASP.NET MVC + WebForms - 路由冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9513750/

相关文章:

xml - Upshot.js 和 LINQ to XML

c# - C# 中 MVC DocuSign WebHook 接收器的正确类是什么

asp.net-mvc - 想要将 asp.net MVC 中的所有请求从给定域重定向到特定 View 页面

c# - 可以在 Asp.Net Web Api 路由中使用句点吗?

c# - MVC 6 路由,SPA 回退 + 404 错误页面

c# - System.Web.Routing.RouteCollection 和 System.Web.Mvc.RouteCollectionExtensions 都具有相同的简单名称 'IgnoreRouteInternal'

c# - 添加 Controller 以编辑配置文件

c# - WebAPI 自动绑定(bind)另一个参数

.net - 本地化-如何在ASP.NET MVC中获取客户端语言环境?

asp.net-mvc - 带有 ASP.NET 的动态 'twitter style' url