簡體   English   中英

如何在ASP.NET MVC 4中將查詢字符串參數轉換為路由

[英]How to convert query string parameters to route in asp.net mvc 4

我有一個正在構建的博客系統,但似乎無法使ASP.NET MVC了解我的路線。

我需要的路由是/ blogs / student / firstname-lastname,因此/ blogs / student / john-doe路由到博客區域,即學生控制者的index操作,該操作需要一個字符串名稱參數。

這是我的路線

routes.MapRoute(
    name: "StudentBlogs",
    url: "blogs/student/{name}",
    defaults: new { controller = "Student", action="Index"}
);

我的控制器動作

public ActionResult Index(string name)
{
    string[] nameparts = name.Split(new char[]{'-'});
    string firstName = nameparts[0];
    string lastName = nameparts[1];

    if (nameparts.Length == 2 && name != null)
    {
      // load students blog from database
    }
    return RedirectToAction("Index", "Index", new { area = "Blogs" });            
}

但這似乎無法解決...它可以與/ blogs / student /?name = firstname-lastname正常工作,但不能使用我想要的路由,即/ blogs / student / firstname-lastname。 任何有關如何解決此問題的建議將不勝感激。

我的RouteConfig

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

        routes.MapRoute(
         name: "StudentBlogs",
         url: "blogs/student/{name}",
         defaults: new { controller = "Student", action = "Index"},
         constraints: new { name = @"[a-zA-Z-]+" },
          namespaces: new string[] { "IAUCollege.Areas.Blogs.Controllers" }
     );

        routes.MapRoute(
            name: "Sitemap",
            url :"sitemap.xml",
            defaults: new { controller = "XmlSiteMap", action = "Index", page = 0}
        );

        //CmsRoute is moved to Gloabal.asax

        // campus maps route
        routes.MapRoute(
            name: "CampusMaps",
            url: "locations/campusmaps",
            defaults: new { controller = "CampusMaps", action = "Index", id = UrlParameter.Optional }
        );


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

        // error routes
        routes.MapRoute(
            name: "Error",
            url: "Error/{status}",
            defaults: new { controller = "Error", action = "Error404", status = UrlParameter.Optional }
        );


        // Add our route registration for MvcSiteMapProvider sitemaps
        MvcSiteMapProvider.Web.Mvc.XmlSiteMapController.RegisterRoutes(routes);
    }
}

您必須在默認路由之前聲明自定義路由。 否則,它將映射到{controller} / {action} / {id}。


Global.asax通常如下所示:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    RouteConfig.RegisterRoutes(RouteTable.Routes);
}

如果您創建了一個名為Blogs的區域,那么將有一個對應的BlogsAreaRegistration.cs文件,如下所示:

public class BlogsAreaRegistration : AreaRegistration 
{
    public override string AreaName 
    {
        get 
        {
            return "Blogs";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context) 
    {
        context.MapRoute(
            "Admin_default",
            "Blogs/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );
    }
}

連字符有時被當作路徑中的正斜杠對待。 當您使用路由blogs/students/john-doe ,我的猜測是它與上面使用blogs/students/john/doe的Area模式匹配,這將導致404。將自定義路由添加到BlogsAreaRegistration.cs文件位於默認路由上方。

嘗試將參數添加到路由:

routes.MapRoute(
    name: "StudentBlogs",
    url: "blogs/student/{name}",
    defaults: new { controller = "Student", action="Index", name = UrlParameter.Optional}
);

嘗試為name參數添加一個約束:

routes.MapRoute(
    name: "StudentBlogs",
    url: "blogs/student/{name}",
    defaults: new { controller = "Student", action="Index" },
    constraints: new { name = @"[a-zA-Z-]+" }
);

短划線有時在MVC中有點怪異..因為它們用於解析下划線。 如果此方法不起作用,我將刪除此答案(盡管應該。)。

如果使用了諸如/blogs/student/12387類的URL,它的另一個好處是無法匹配路由。

編輯:

如果你有相同名稱的控制器..你需要在無論是在各區域的路線命名空間。 控制器在哪里都無所謂,即使在單獨的區域中也是如此。

嘗試將適當的名稱空間添加到處理Student控制器的每個路由中。 有點像這樣:

routes.MapRoute(
    name: "StudentBlogs",
    url: "blogs/student/{name}",
    defaults: new { controller = "Student", action="Index" },
    namespaces: new string[] { "Website.Areas.Blogs.Controllers" }
);

..也許是管理區域中一個的Website.Areas.Admin.Controllers

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM