配置 asp.net mvc hocalhost/Products.aspx 到 hocalhost/Products

发布于 2024-11-01 17:57:31 字数 196 浏览 3 评论 0原文

如何配置 asp.net mvc 路由以将 301

hocalhost/Products.aspx 和 hocalhost/Search.aspx

永久重定向到

hocalhost/Products 和 hocalhost/Search

,即从路径中删除 .aspx 扩展名?

How to configure asp.net mvc routing to redirect permanently 301

hocalhost/Products.aspx and hocalhost/Search.aspx

to

hocalhost/Products and hocalhost/Search

i.e. to remove .aspx extension from the path?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情感失落者 2024-11-08 17:57:31

按照这些思路做一些事情应该可以解决问题。映射以下路由:

routes.MapRoute("Redirect route", "{file}.aspx", 
    new { controller = "home", action = "redirect" });

并在控制器中定义一个 Redirect 操作:

public ActionResult Redirect()
{
    // use Request.RawUrl, for instance to parse out what was invoked

    // this regex will extract anything between a "/" and a ".aspx"
    var regex = new Regex(@"(?<=/).+(?=\.aspx)", RegexOptions.Compiled);
    var action = regex.Match(Request.RawUrl);

    return RedirectToActionPermanent(action.Value);
}

您可以将两个 aspx 页面重定向到同一重定向路由,并通过解析 < code>HttpContext.Request.RawUrl (不过对于最后一点可能有更好的方法)。

更新

确实有一种更简单的方法,正如@alex自己发现的那样。为了获取原始请求中的文件,只需执行以下操作:

string file = RouteData.Values["file"].ToString();

Something along these lines should do the trick. Map the following route:

routes.MapRoute("Redirect route", "{file}.aspx", 
    new { controller = "home", action = "redirect" });

And define a Redirect action in your controller:

public ActionResult Redirect()
{
    // use Request.RawUrl, for instance to parse out what was invoked

    // this regex will extract anything between a "/" and a ".aspx"
    var regex = new Regex(@"(?<=/).+(?=\.aspx)", RegexOptions.Compiled);
    var action = regex.Match(Request.RawUrl);

    return RedirectToActionPermanent(action.Value);
}

You could redirect both aspx pages to the same redirect route and detect which file has actually been invoked by parsing HttpContext.Request.RawUrl (there might be a better way for this last point though).

UPDATE

There is indeed a simpler way, as found out by @alex himself. In order to get the file in the original request, just do:

string file = RouteData.Values["file"].ToString();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文