asp.net多个url指向同一个控制器
我正在一个社交网站上工作,我们正在其中实现个人资料、页面、群组等。在此阶段,我们正在使用个人资料和页面。他们都有墙,用户可以在其中放置一些状态、图片等,更像 Facebook 墙。
现在控制器 WallController 可以通过两个不同的 url 访问。
www.mysite.com/profile/121/some-user/wall
and
www.mysite.com/page/222/some-page/wall
在页面的左侧,我加载一些基本信息(名称等)和菜单。 说
www.mysite.com/profile/121/some-user/photos
www.mysite.com/profile/121/some-user/videos
www.mysite.com/profile/121/some-user/songs
这适用于两者(页面和个人资料)。
的路线
routes.MapRoute(
"Page-wall", // Route name
"page/{id}/{name}/wall", // URL with parameters
new { controller = "wall", action = "details", id = "", name = "" },
new { id = @"\d+" },
new string[] { "PagesNameSpace.Controllers" } // Parameter defaults
);
这是我的页面和配置文件
routes.MapRoute(
"profile-wall", // Route name
"profile/{id}/{name}/wall", // URL with parameters
new { controller = "wall", action = "details", id = "", name = "" },
new { id = @"\d+" },
new string[] { "ProfileNameSpace.Controllers" } // Parameter defaults
);
现在,问题是,我必须确定哪个对象正在访问该网址。这是我的 WallController,
public class WallController : Controller
{
public ActionResult Details(long id, string name)
{
return View(LoadWallData(id));
}
}
我将路由值字典视为解决方案,但想了解这种情况的最佳解决方案是什么。
帮助将不胜感激。
问候 帕明德
I am working on a social networking site where we are implementing profiles, pages, groups etc. At this stage, we are working with profiles and pages. They both have wall where user can put some status, pics etc, more like facebook wall.
Now a controller WallController can be accessed by two different urls.
www.mysite.com/profile/121/some-user/wall
and
www.mysite.com/page/222/some-page/wall
On the left hand side of the page, I load some basic information (name etc) and a menu.
saying
www.mysite.com/profile/121/some-user/photos
www.mysite.com/profile/121/some-user/videos
www.mysite.com/profile/121/some-user/songs
This applies to both (page and profile).
here is my route for page
routes.MapRoute(
"Page-wall", // Route name
"page/{id}/{name}/wall", // URL with parameters
new { controller = "wall", action = "details", id = "", name = "" },
new { id = @"\d+" },
new string[] { "PagesNameSpace.Controllers" } // Parameter defaults
);
and for profile
routes.MapRoute(
"profile-wall", // Route name
"profile/{id}/{name}/wall", // URL with parameters
new { controller = "wall", action = "details", id = "", name = "" },
new { id = @"\d+" },
new string[] { "ProfileNameSpace.Controllers" } // Parameter defaults
);
Now, the problem is, I have to identify what object is accessing the url. here is my WallController
public class WallController : Controller
{
public ActionResult Details(long id, string name)
{
return View(LoadWallData(id));
}
}
I see a route value dictionary as a solution, but would like to see, what is the best solution for this kind for situations.
help will be appreciated.
Regards
Parminder
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我可能会执行以下操作,您只需将值添加到您的路线中:
更改您的控制器:
然后您的路线:
I would probably do the following, you just add the values into your routes:
Change your controller:
And then your routes:
使用
((System.Web.Routing.Route)(Url.RequestContext.RouteData.Route)).Url
您可以从 MapRoute 获取带有参数值的 URL。using
((System.Web.Routing.Route)(Url.RequestContext.RouteData.Route)).Url
you can get the URL with parameter values from MapRoute.我觉得,我会采用这种方法。
和我的路线
现在只需传递类型参数,我就可以获得页面和个人资料的操作链接。
非常感谢大家。
问候
I feel, I would go with this approach.
and my routes
Now just by passing the type parameter, I can get action link for page and profile.
thanks a lot to everyone.
Regards