2012-02-22 33 views
1

我正在一个社交网站上,我们正在实施配置文件,页面,组等。在这个阶段,我们正在处理配置文件和页面。他们都有墙壁,用户可以放置一些地位,图片等,更像Facebook墙。asp.net多个网址指向相同的控制器

现在可以通过两个不同的URL访问控制器WallController。

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 
      ); 

现在的问题是,我必须确定哪些对象被访问的URL路径。这里是我的WallController

public class WallController : Controller 
{ 
    public ActionResult Details(long id, string name) 
    { 

     return View(LoadWallData(id)); 
    } 

} 

我看到一个路径值字典作为一个解决方案,但希望看到的,什么是这种情况的最佳解决方案。

帮助将不胜感激。

问候 成员Parminder

回答

1

我可能会做到以下几点,你只需要添加值到您的路线:

更改您的控制器:

public class WallController : Controller 
{ 
    public ActionResult Details(long id, string name, string obj)//added param 
    { 

     return View(LoadWallData(id)); 
    } 
} 

然后你的路线:

routes.MapRoute(
      "Page-wall", // Route name 
      "page/{id}/{name}/wall", // URL with parameters 
      new { controller = "wall", action = "details", id = "", name = "", 
                /*See this>>>> */ obj="page"}, 
      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 = "", 
              /*See this>>>> */ obj="profile" }, 
      new { id = @"\d+" }, 
      new string[] { "ProfileNameSpace.Controllers" } 
      ); 
0

使用((System.Web.Routing.Route)(Url.RequestContext.RouteData.Route)).Url您可以从MapRoute获取带有参数值的网址。

0

我觉得,我会用这种方法。

public class WallController : Controller 
{ 
public ActionResult Details(string type ,long id, string name)//added param 
{ 

    return View(LoadWallData(id)); 
} 
} 

和我的路线

routes.MapRoute(
     "wall-default", // Route name 
     "{type}/{id}/{name}/wall", // URL with parameters 
     new { controller = "wall", action = "details", id = "", name = "", 
                   type="profile"}, 
     new { id = @"\d+" }, 
     new string[] { "PagesNameSpace.Controllers" } // Parameter defaults 
     ); 

现在只需通过传递类型参数,我就能采取行动链接,页面和配置文件。

非常感谢大家。

Regards

相关问题