【问题标题】:Mvc 4 Routing based on Group基于组的 Mvc 4 路由
【发布时间】:2016-06-14 09:30:11
【问题描述】:
我的登录页面是http://localhost/account/login。用户登录后,我打算根据组对用户进行分类。
第 1 组将有
http://localhost/group1/home/index
第 2 组将有
http://localhost/group2/home/index
只要指出我正确的方向,它是否涉及 Mvc.Area?抱歉,对 MVC 完全陌生。
【问题讨论】:
-
您可以使用与this answer 类似的方法将用户在登录后重定向到特定 URL,并在注销后返回到一般 URL。您可以使用区域,但从您的问题中不清楚每个组的操作是否相同或不同。如果它们都完全相同,您可以通过路由完成所需的一切。如果不是,那么区域(或者,MvcCodeRouting)将是更好的选择。
标签:
asp.net-mvc-4
asp.net-mvc-routing
【解决方案1】:
试试这个
更改 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();
}
}
}