Asp.net Mvc 2 如何重新路由到 www.example.com/my-profile
我想将此地址 www.exmaple.com/my-profile 重新路由到我的 mvc 应用程序中的配置文件控制器。有我的 global.aspx,默认路由是
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}/", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
这是我试图用来重新路由到 www.exmaple.com/my-profile 的 Maproute
routes.MapRoute(
"Profile", // Route name
"{profile_name}/", // URL with parameters
new { controller = "Portfolio", action = "Index", page = UrlParameter.Optional } // Parameter defaults
);
问题是当我输入 www.exmaple.com/profile 时它试图将我引导到默认的映射路由,而不是我指定的映射路由。
但是,当我可以执行此操作 www.example.com/profile/my-profile 时,
routes.MapRouteLowercase(
"Profile", // Route name
"profile/{profile_name}/", // URL with parameters
new { controller = "Portfolio", action = "Index", page = UrlParameter.Optional } // Parameter defaults
);
它工作正常,但我不想在 my-profile 之前添加配置文件。我希望它像 facebook.com/my-profile 或 youtube.com/my-profile 一样工作。
有谁知道我是如何实现这一目标的,我已经寻找解决方案两个多月了(时断时续)
谢谢
i would like to reroute this address www.exmaple.com/my-profile to a profile controller i have in my mvc application. There is my global.aspx, as you can the the default routing is
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}/", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
This is the Maproute i'm trying to use to reroute to www.exmaple.com/my-profile
routes.MapRoute(
"Profile", // Route name
"{profile_name}/", // URL with parameters
new { controller = "Portfolio", action = "Index", page = UrlParameter.Optional } // Parameter defaults
);
The problem is when i type www.exmaple.com/profile it trys to direct me to the default maproute, not the one i have specified.
But when i can do this www.example.com/profile/my-profile
routes.MapRouteLowercase(
"Profile", // Route name
"profile/{profile_name}/", // URL with parameters
new { controller = "Portfolio", action = "Index", page = UrlParameter.Optional } // Parameter defaults
);
it works fine, but i dont want to add profile before my-profile. I would like it to work like facebook.com/my-profile or youtube.com/my-profile.
Does anyone one know how i accomplish this, i have been looking for a solution for over 2 months (on and off)
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我没记错的话,您的路由将返回 URL 的第一个匹配模式。我希望在你的情况下,你的 URL 首先匹配你的默认模式。
将更具体的路线移至“默认”路线上方,看看这是否有帮助。
Your routing if I remember rightly will return the first matching pattern for the URL. I would expect in your case, your URL is matching your default pattern first.
Move your more specific route above your 'default' route and see if this helps.