ASP.NET MVC C#路由 - 传递一个空整数 [英] ASP.NET MVC C# Routing - Passing a null integer

查看:143
本文介绍了ASP.NET MVC C#路由 - 传递一个空整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与MVC 3个工作在一个Web应用程序,我在路由面临的一个问题。

I'm working with MVC 3 in a web app and i'm facing a problem in routing.

我定义这样我的路由器处理程序:

I'm defining my router handler like this:

           routes.MapRoute(
           "Users", 
           "{controller}.aspx/{action}/{id}/{page}", // URL with parameters
           new { controller = "Users", action = "Details", id = UrlParameter.Optional, page = UrlParameter.Optional } // Parameter defaults
       );

网址是: HTTP://app.domain/Users.aspx/Details/ 5分之114142(例如)

我sucefully获得用户的ID,但我不能得到的页码。

I'm sucefully getting the id of the user, but i can't get the page number.

用户控制器初始化是这样的:

The controller of users is initialized like this:

           public ActionResult Details(long id, int? page)

页面总是返回NULL(我需要的页面为空整数)。

The page is always returning null (i need the page as a null integer).

和我确定的路线错了?

感谢

推荐答案

ID 不能是可选的,如果是可选的。只有在最后路由定义的参数可以是可选的。

id cannot be optional if page is optional. Only the last parameter of a route definition can be optional.

所以:

routes.MapRoute(
    "Users", 
    {controller}.aspx/{action}/{id}/{page}",
    new { 
        controller = "Users",  
        action = "Details", 
        page = UrlParameter.Optional 
    }
);

和则: /Users.aspx/Details/114142/5 将成功映射到

public ActionResult Details(long id, int? page)
{
    ...
}

这篇关于ASP.NET MVC C#路由 - 传递一个空整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆