0

我的 RouteConfig,cs 文件中有以下路由:

routes.MapRoute(
    name: "SectionHomePage",
    url: "{SectionType}/{SectionID}",
    constraints: new { SectionType = @"(Books|Cinema|Collections|Games)" },
    defaults: new { controller = "SectionHomePageController" }
    );

如果可能的话,我想做的是将action参数重命名为SectionType或以某种方式分配SectionTypeaction参数。(我知道我可以重命名SectionType为,action但为了便于阅读,我想保持命名)。

控制器:

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

namespace FFInfo.WebUI.Controllers
{
    public class SectionHomePageController : Controller
    {
        // GET: /SectionHomePage/
        public ActionResult Games()
        {
            //ViewBag.Head = RouteData.Values["id"];
            return View();
        }
    }
}

错误:

RouteData 必须包含一个名为 'action' 且具有非空字符串值的项目。

4

2 回答 2

0

需要操作,但是您可以在路线中设置操作

routes.MapRoute(
    name: "SectionHomePage",
    url: "{SectionType}/{SectionID}",
    constraints: new { SectionType = @"(Books|Cinema|Collections|Games)" },
    defaults: new { controller = "SectionHomePageController", action="Section" }
);

这假设您有一个 SectionHomePageController ,其操作类似于此

public ActionResult Section(string sectionType, int sectionId) 
{
    // can use a switch...case instead
    if (sectionType == "Books")
       return Books(sectionId);
}
public ActionResult Books(int sectionId)
{
    var model = GetBooksModel(); // load stuff
    return View("Books", model); // Set view and provide model
}

或者,您可以

routes.MapRoute(
    name: "SectionHomePage",
    url: "{action}/{SectionID}",
    constraints: new { action = @"(Books|Cinema|Collections|Games)" },
    defaults: new { controller = "SectionHomePageController" }
);

与 actionResult 的

public ActionResult Books(int sectionId) 
{
    var model = GetBooksModel(); // load stuff
    return View("Books", model); // Set view and provide model
}

public ActionResult Cinema(int sectionId) 
{
    ...
}
于 2013-07-16T02:24:09.913 回答
0

无法重命名 ACTION 元素。

于 2013-10-18T22:33:45.753 回答