路由遗留请求使用和不使用查询字符串

编程入门 行业动态 更新时间:2024-10-27 02:28:09
本文介绍了路由遗留请求使用和不使用查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

(开始之前:我知道这并的这个。我想找到 - 如果possible-一个更简洁的解决方案,一个稍微具体的问题)

(Before starting: I am aware of this and this. I'd like to find a more concise solution -if possible- for a slightly more specific problem)

我改写MVC的旧Web表单应用程序。像往常一样,没有固定链接应该被打破。

I'm rewriting an old Webforms app in MVC. As usual, no permalinks should be broken.

我使用的是标准的 {控制器} / {行动} / {ID} 路线。传统路径通常是 SomePage.aspx页面?ID = XXX ,我有一个特别的情况下 Foo.aspx 是一个和:酒吧( /酒吧或 /酒吧/索引新的URL)名单  Foo.aspx ID = XXX 是酒吧详细(新网址: /酒吧/查看/ XXX )

I'm using standard {controller}/{action}/{id} routes. Legacy paths are usually SomePage.aspx?ID=xxx, and I have one particular case where Foo.aspx is a list of Bar (new URL: /Bar or /Bar/Index) and Foo.aspx?ID=xxx is the Bar detail (new URL: /Bar/View/xxx)

一个解决办法可能是默认的图路线之前加入以下

One possible workaround is adding the following before the Default MapRoute:

routes.MapRoute("Bar View", "Foo.aspx", new { controller = "Bar", action = "View" });

然后定义在BarController相应的动作:

And then defining the corresponding action in BarController:

public ActionResult View(int? id) { if (id == null) return RedirectToAction("Index"); return View(); }

有两个问题:

  • 现在,如果我创建一个ActionLink的,它采用了传统的格式
  • 我想在路线来处理这一点;使得ID可为空,并在控制器重定向是错误

我很好用手工绘制传统的URL(我并不需要一个通用的解决方案,只有大约8页)

I'm fine with mapping the legacy URLs by hand (I don't need a generic solution and there are only about 8 pages)

这是一个新的项目,所以我不依赖于任何东西。

This is a new project, so I'm not tied to anything.

推荐答案

我能够根据危险'的想法加上基于这个答案。

我的新路由表是:

routes.MapRoute("Bar", "Bar/{action}/{id}", new { controller = "Bar", action = "Index", id = UrlParameter.Optional }); routes.MapRoute("Bar View", "Foo.aspx", new {controller = "Bar", action = "View"}, new {id = new QueryStringConstraint()}); routes.MapRoute("Bar Index", "Foo.aspx", new { controller = "Bar", action = "Index" }); routes.MapRoute("Default", /*...*/);

而QueryStringConstraint简单得不能再简单:

And the QueryStringConstraint couldn't be simpler:

public class QueryStringConstraint : IRouteConstraint { public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { return httpContext.Request.QueryString.AllKeys .Contains(parameterName, StringComparer.InvariantCultureIgnoreCase); } }

更多推荐

路由遗留请求使用和不使用查询字符串

本文发布于:2023-11-13 01:16:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583005.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   字符串

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!