asp.net多个url指向同一个控制器

发布于 2025-01-07 12:35:18 字数 1520 浏览 0 评论 0原文

我正在一个社交网站上工作,我们正在其中实现个人资料、页面、群组等。在此阶段,我们正在使用个人资料和页面。他们都有墙,用户可以在其中放置一些状态、图片等,更像 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 技术交流群。

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

发布评论

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

评论(3

酷到爆炸 2025-01-14 12:35:18

我可能会执行以下操作,您只需将值添加到您的路线中:

更改您的控制器:

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

I would probably do the following, you just add the values into your routes:

Change your controller:

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

        return View(LoadWallData(id));
     }
}

And then your routes:

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" }
           );
对风讲故事 2025-01-14 12:35:18

使用 ((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.

红ご颜醉 2025-01-14 12:35:18

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

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

现在只需传递类型参数,我就可以获得页面和个人资料的操作链接。

非常感谢大家。

问候

I feel, I would go with this approach.

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

    return View(LoadWallData(id));
 }
 }

and my routes

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

Now just by passing the type parameter, I can get action link for page and profile.

thanks a lot to everyone.

Regards

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