MVC 中的默认数字根路由

发布于 2025-01-05 05:02:41 字数 707 浏览 3 评论 0原文

我有一个网站,我想要

/22         Redirect to 
/user/22

等等,但是还有其他 mvc 视图和控制器,它们都工作正常,我使用了以下路线,但它不起作用。

routes.MapRoute(
    "Final",
    "{id}",
    new { controller = "Root", action = "Index"},
    new { id = @"\d+" },
    new string[] { "MyWebApp.Controllers" }
);

理想情况下,此路由仅在 url 片段为数字时才有效。

我在 MyWebApp.Controllers 命名空间中也有一个 RootController 。它所做的只是重定向到其他页面,如下所示,

public class RootController : Controller
{
    public ActionResult Index(long id) {
        return RedirectPermanent("/user/" + id);
    }
}

现在,我们必须这样做,因为它是旧网站的升级,我们无法更改 url 方案,因为它是公开的并且正在使用。

注意:URL /user/22 等工作正常,只有这个根 URL 出现问题。

I have a website and I want

/22         Redirect to 
/user/22

etc and so on, however there are other mvc views and controllers and they all work alright, I have used following Route but its not working.

routes.MapRoute(
    "Final",
    "{id}",
    new { controller = "Root", action = "Index"},
    new { id = @"\d+" },
    new string[] { "MyWebApp.Controllers" }
);

Ideally this route should only work if url fragment is numeric.

I have a RootController in MyWebApp.Controllers namespace as well. And all it does is redirect to other page as below,

public class RootController : Controller
{
    public ActionResult Index(long id) {
        return RedirectPermanent("/user/" + id);
    }
}

Now, we have to do this because it is an upgrade to old website and we cannot change url scheme, because its public and in use.

Note: URLs /user/22 etc are working correctly, only this root URL is giving problem.

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

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

发布评论

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

评论(1

梦一生花开无言 2025-01-12 05:02:41

我已经测试了这条路线:

        routes.MapRoute(
            "Final",
            "{id}",
            new { controller = "Root", action = "Index" },
            new { id = @"\d+" }
        );

它正常工作。但如果您遇到问题,我猜测您所需的 URL 与之前的另一个路由匹配。将此路线作为您的第一条路线,看看是否可以解决问题。

例如,如果您的路线如下所示:

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

        routes.MapRoute(
            "Final",
            "{id}",
            new { controller = "Root", action = "Index" },
            new { id = @"\d+" }
        );

您将收到404 未找到资源。但是,如果您像这样切换它们:

        routes.MapRoute(
            "Final",
            "{id}",
            new { controller = "Root", action = "Index" },
            new { id = @"\d+" }
        );

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

那么您将通过 /1234 之类的请求获得所需的路由。

I have tested this route out:

        routes.MapRoute(
            "Final",
            "{id}",
            new { controller = "Root", action = "Index" },
            new { id = @"\d+" }
        );

It is working just as it should. But if you're having an issue with it, I'm guessing that your desired URL is matching another route prior to it. Put this route as your first route and see if that fixes it.

For instance, if your routes look like this:

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

        routes.MapRoute(
            "Final",
            "{id}",
            new { controller = "Root", action = "Index" },
            new { id = @"\d+" }
        );

You will get a 404 resource not found. But if you switch them like this:

        routes.MapRoute(
            "Final",
            "{id}",
            new { controller = "Root", action = "Index" },
            new { id = @"\d+" }
        );

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

Then you'll get the desired routing with a request like /1234.

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