2016-06-14 100 views
0

我的登錄頁面是http://localhost/account/login。用戶登錄後,我打算根據組對用戶進行分類。Mvc 4基於羣組的路由

第1組有

http://localhost/group1/home/index 

第2組將有

http://localhost/group2/home/index 

只需指向我到正確的方向,是它涉及Mvc.Area?對不起,MVC是全新的。

+0

您可以使用類似的方法來[這個答案](https:// stack overflow.com/a/37022067/181087)在登錄後將用戶重定向到特定的URL,並在註銷後返回到通用URL。你*可以*使用區域,但從你的問題來看,不清楚每個組的行爲是相同的還是不同的。如果它們完全相同,則可以通過路由完成所需的一切。如果不是,那麼區域(或者[MvcCodeRouting](http://mvccoderouting.codeplex.com/))將是更好的選擇。 – NightOwl888

回答

0

嘗試這個

更改App_Start/RouteConfig.cs

public class RouteConfig 
    { 
     public static void RegisterRoutes(RouteCollection routes) 
     { 
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  

      routes.MapMvcAttributeRoutes();//You Can Add Manually 

      routes.MapRoute(
       name: "Default", 
       url: "{controller}/{action}/{id}", 
       defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
      ); 
     } 
    } 

然後喲可以修改控制器

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 

namespace WebApplication1.Controllers 
{ 
    public class HomeController : Controller 
    { 
     // GET: Home 
     public ActionResult Index() 
     { 
      return View(); 
     } 
     // http://localhost:52603/group1/home/index 
     [Route("group1/home/index")] 
     public ActionResult Group1() 
     { 
      return View(); 
     } 
     //http://localhost:52603/group2/home/index 
     [Route("group2/home/index")] 
     public ActionResult Group2() 
     { 
      return View(); 
     } 
    } 
}