.NET MVC2 如何更改传入的 URL 参数

发布于 2024-10-06 02:54:30 字数 884 浏览 1 评论 0原文

我正在使用 .NET MVC 2 如果传入请求包含 URL:

http://something.com/1234

其中 1234 是 {id} 参数。我希望能够使用 id 从数据库获取一些数据,然后更改 URL,以便它转到有效的控制器和操作。

新的 URL 应类似于:

http://something.com/area/username/controller/action/id

在数据库中查找原始 id (1234),数据将转换为特定的 {username}/{controller}/{action}/{id}。

我在 AreaRegistration 类中设置了以下路由:

context.MapRoute(
    "route1",
    "area/{controller}/{action}/{id}",
    new { action = "Index", controller = "Home" },
    new string[] { "MyApp.Areas.Controllers" }
        );

context.MapRoute(
    "route2",
    "area/{controller}/{id}",
    new { action = "Index", controller = "Home" },
    new string[] { "MyApp.Areas.Controllers" }
);

我似乎无法弄清楚如何/在哪里查找数据库数据并更改/重写 URL。我尝试实现自定义 RouteHandler 和 RouteBase 但似乎都没有达到我的需要。

这是我的第一篇帖子,所以如果我的问题不清楚,请原谅我。任何建议表示赞赏。

I'm using .NET MVC 2
If an incoming request contains the URL:

http://something.com/1234

where 1234 is an {id} parameter. I want to be able to use the id to get some data from a database and then change the URL so that it goes to a valid controller and action.

The new URL should look something like:

http://something.com/area/username/controller/action/id

where the original id (1234) is looked up in the database and the data would translate to a specific {username}/{controller}/{action}/{id}.

I have the following routes set up in an AreaRegistration class:

context.MapRoute(
    "route1",
    "area/{controller}/{action}/{id}",
    new { action = "Index", controller = "Home" },
    new string[] { "MyApp.Areas.Controllers" }
        );

context.MapRoute(
    "route2",
    "area/{controller}/{id}",
    new { action = "Index", controller = "Home" },
    new string[] { "MyApp.Areas.Controllers" }
);

What I can't seem to figure out is how/where to lookup the database data and change/rewrite the URL. I have tried implementing a custom RouteHandler and RouteBase but neither seem to do what I need.

This is my first SO post so forgive me if my question isn't clear. Any suggestions are appreciated.

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

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

发布评论

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

评论(1

仅此而已 2024-10-13 02:54:31

您需要返回一个 RedirectToAction() 来执行 URL 重写...

return this.RedirectToAction(action, controller);

有一大堆用于指定 ids、路由值等的重载...

至于在数据库中查找它,这将取决于您的数据访问模型。假设使用 Entity Framework 或 Linq,它会类似于:

DataClasses1DataContext dc = new DataClasses1DataContext();

var record = from a in dc.GetTable<Order>() select id, username;

为了阐明 MVC 的工作原理...

我采用类似 http://example.com/controller/action/idhttp 的 URL: //example.com/area/controller/action/id

并在指定的控制器上调用适当的操作方法。通常您会返回一个视图,但是,您可以发送许多特殊数据类型以获取不同的结果,例如 JSON 数据、HTTP 重定向等。

如果 URL 中省略了区域/控制器/操作,则使用路由中的默认值。

所以...

如果您只想显示适当的页面,则可以将 URL 保留为 http://example.com/1234 只要默认操作/控制器具有显示该页面的代码即可适当的看法。

如果您出于美观原因想要更改 URL,则可以让默认控制器/操作接受 Id 并返回一个 RedirectToAction,它指向您想要的 URL 的控制器/操作。

值得注意的是,它会生成尽可能最小的 URL,因此如果您要在默认控制器上执行 2 个操作:

HomeController -> Index(int id)
HomeController -> ShowDetails(int id)

索引的 URL 将类似于

http://example.com/1234

重定向到 ShowDetails 会给出如下 URL:

http://example.com/ShowDetails/1234

如果 ShowDetails 位于不同的 (非默认)控制器,您会得到类似这样的内容:

http://example.com/OtherController/1234

假设路由遵循标准 /Controller/Action/Id 格式。不用说,通过注册不同的路由,它会根据需要交换参数。

希望有帮助吗?

You need to return a RedirectToAction() to perform the URL rewrite...

return this.RedirectToAction(action, controller);

There are a whole host of overloads for specifying ids, route values, etc...

As to looking it up in the database, that will depend on your data access model. Assuming Entity Framework or Linq it would be something like:

DataClasses1DataContext dc = new DataClasses1DataContext();

var record = from a in dc.GetTable<Order>() select id, username;

To clarify how MVC works...

I takes a URL like http://example.com/controller/action/id or http://example.com/area/controller/action/id

and calls the appropriate action method on the specified controller. Normally you would return a view however, you can send a lot of special data types back for different results eg JSON Data, HTTP Redirects, etc.

If an area/controller/action is omitted in the URL, the defaults from the route are used.

So...

If you just want to show the appropriate page, you can leave the URL as http://example.com/1234 as long as the default action/controller has the code to display the appropriate view.

If you want to change the URL for aesthetic reasons, you would have the default controller/action take in an Id and return a RedirectToAction which points at the Controller/Action for the URL you want.

It's worth noting that it will generate the most minimal URL possible so if you were to have 2 actions on your default controller:

HomeController -> Index(int id)
HomeController -> ShowDetails(int id)

the URL for index would be something like

http://example.com/1234

redirecting to ShowDetails would give a URL like this:

http://example.com/ShowDetails/1234

If ShowDetails were on a different (non-default) controller you'd get something like this:

http://example.com/OtherController/1234

That's assuming the routes follow the standard /Controller/Action/Id format. Needless to say by registering different routes, it swaps the parameters around as appropriate.

Hope that helps?

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