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

Asp.net mvc .NET MVC2如何更改传入的URL参数,asp.net-mvc,asp.net-mvc-2,asp.net-mvc-routing,Asp.net Mvc,Asp.net Mvc 2,Asp.net Mvc Routing,我正在使用.NETMVC2 如果传入请求包含URL: http://something.com/1234 其中1234是{id}参数。我希望能够使用id从数据库中获取一些数据,然后更改URL,使其转到有效的控制器和操作 新URL应类似于: http://something.com/area/username/controller/action/id DataClasses1DataContext dc = new DataClasses1DataContext(); var record

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

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

新URL应类似于:

http://something.com/area/username/controller/action/id
DataClasses1DataContext dc = new DataClasses1DataContext();

var record = from a in dc.GetTable<Order>() select id, username;
在数据库中查找原始id(1234),数据将转换为特定的{username}/{controller}/{action}/{id}

我在区域注册课程中设置了以下路线:

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,但它们似乎都不能满足我的需要


这是我的第一篇SO帖子,如果我的问题不清楚,请原谅我。非常感谢您的建议。

您需要返回一个
重定向到操作()
以执行URL重写

return this.RedirectToAction(action, controller);
有一大堆重载用于指定ID、路由值等

至于在数据库中查找它,这将取决于您的数据访问模型。假设实体框架或Linq是这样的:

http://something.com/area/username/controller/action/id
DataClasses1DataContext dc = new DataClasses1DataContext();

var record = from a in dc.GetTable<Order>() select id, username;
索引的URL类似于

http://example.com/1234
重定向到ShowDetails会得到如下URL:

http://example.com/ShowDetails/1234
http://example.com/OtherController/1234
如果ShowDetails位于不同的(非默认)控制器上,则会得到如下结果:

http://example.com/ShowDetails/1234
http://example.com/OtherController/1234
假设路由遵循标准的
/Controller/Action/Id
格式。不用说,通过注册不同的路由,它可以适当地交换参数


希望有帮助?

您需要返回一个
重定向到操作()
来执行URL重写

return this.RedirectToAction(action, controller);
有一大堆重载用于指定ID、路由值等

至于在数据库中查找它,这将取决于您的数据访问模型。假设实体框架或Linq是这样的:

http://something.com/area/username/controller/action/id
DataClasses1DataContext dc = new DataClasses1DataContext();

var record = from a in dc.GetTable<Order>() select id, username;
索引的URL类似于

http://example.com/1234
重定向到ShowDetails会得到如下URL:

http://example.com/ShowDetails/1234
http://example.com/OtherController/1234
如果ShowDetails位于不同的(非默认)控制器上,则会得到如下结果:

http://example.com/ShowDetails/1234
http://example.com/OtherController/1234
假设路由遵循标准的
/Controller/Action/Id
格式。不用说,通过注册不同的路由,它可以适当地交换参数


希望这能有所帮助?

也许我把这件事弄得比我需要的更困难了。可能是单个参数URL的默认路由,然后从那里重定向到操作。没那么容易!是的,这就是它的简单程度。每次我写一些MVC代码时,我坐在那里盯着它想“真的是这样吗?”——10次中有9次是这样:)也许我把这件事弄得比我需要的更难了。可能是单个参数URL的默认路由,然后从那里重定向到操作。没那么容易!是的,这就是它的简单程度。每次我写一些MVC代码时,我坐在那里盯着它想“真的是这样吗?”——10次中有9次是这样:)