路由问题MVC4

时间:2015-05-11 06:47:51

标签: c# asp.net-mvc asp.net-mvc-4

我想让我的网址为/ {page number}作为我的主页,例如localhost:50915/1作为第1页的记录列表,我的URL路由如下:

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

        routes.MapRoute(
            name: "EditForm",
            url: "{controller}/{formid}",
            defaults: new { controller = "ManageForm", action = "Update", id = UrlParameter.Optional }
        );

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

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

第二个MapRoute方法是我为执行所需操作而添加的方法。它工作得非常好,但每当我尝试访问另一个Controller" ManageController"例如localhost:50915 / ManageForm它返回主页(显示记录列表的同一页面)。

以下是我的解决方案的快照:

Solution Snapshot

每当我尝试删除为主页分页执行此URL重新路由的代码行时,一切正常,但当然URL将为localhost:50915?page = 1

我的HomeController如下:

public class HomeController : Controller
{
    //
    // GET: /Home/
    [HttpGet]
    public ViewResult Index(int page = 1)
    {
        const int pageSize = 4;
        FormCollection forms = FormService.GetAllActiveForms(page, pageSize);

        var formModel = new FormListViewModel
        {
            Forms = forms,
            PagingInfo = new PageInfo
            {
                PageNumber = page,
                PageSize = pageSize,
                TotalItems = forms.Count > 0 ? forms[0].TotalRecord : 0
            }
        };

        return View(formModel);
    }
}

非常感谢,谢谢。

2 个答案:

答案 0 :(得分:1)

您无法使用localhost:50915/ManageForm,因为它与其他路由定义冲突。当您的路由参数是可选的时,MVC看不到"{controller}/{formid}""{page}"之间的区别。

您必须使用第一条路线localhost:50915/ManageForm/{formId}才能使其正常运行。

答案 1 :(得分:0)

解决方案是将管理表单的新路由映射到url / ManageForm / Create

我更改了RouteConfig.cs,如下所示:

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

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

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

        //ManageForm/{formid}
        routes.MapRoute(
            name: "EditForm",
            url: "{controller}/{formid}",
            defaults: new { controller = "ManageForm", action = "Update", id = UrlParameter.Optional }
        );

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