正在使用的查询字符串id参数不 [英] QueryString id parameter not being used

查看:98
本文介绍了正在使用的查询字符串id参数不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很基本的ASP.Net MVC项目,我想使用ID的参数名对我的控制器动作之一。从一切我读过,不应该是一个问题,但使用的ID参数名因故未能获得价值从查询字符串中提取,但如果将它更改为任何其他不同的名称,它会工作。

I've got a very basic ASP.Net MVC project where I'd like to use a parameter name of id on one of my controller actions. From everything I've read that shouldn't be a problem but for some reason using a parameter name of id fails to get the value extracted from the query string but if I change it to any other different name it will work.

我只在我的global.asx一个路由

I only have a single route in my global.asx

routes.MapRoute(
    "Default",                                              // Route name
    "{controller}/{action}/{id}",                           // URL with parameters
    new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
);

我的控制器的方法是:

My controller method is:

public ActionResult Confirm(string id)
{
     ....
}

的URL http:// mysite的/客户/确认/ ABCD 工作。的网址的http:// mysite的/客户/确认ID = ABCD 失败

如果我改变控制器的方法:

If I change the controller method to:

public ActionResult Confirm(string customerID)
{
     ....
}

然后的URL http:// mysite的/客户/确认的customerID = ABCD 作品

是不是有什么特别的地方使用ID作为一个ASP.Net MVC的查询字符串参数?

Is there something special about using "id" as a parameter in an ASP.Net MVC query string?

更新:从1234到ABCD改变了ID,我的身份证的实际上是字符串

Update: Changed id from 1234 to abcd, my id's are actually strings.

推荐答案

如果你不申请一个ID参数(无论是查询字符串或POST),则系统会忽略它,你可以在你的控制器中删除ID参数

If you do not apply an id parameter (either querystring or POST), the system just ignores it, and you can remove the "id" parameter in your controller:

public ActionResult Confirm()

在你的情况,你只需坚持使用ID参数。为什么让一个丑陋的customerID参数,当ID为映射自动?

In your case, you would just stick with the id parameter. Why make an ugly customerID parameter, when id is "mapped" automatically?

这是采用id参数的一个容易和简单的例子。

This is an easy and simple example of the use of id parameter.

public ActionResult Confirm(int? id)
{
     if (id.HasValue && id.Value > 0) // check the id is actually a valid int
         _customerServer.GetById(id.Value);

    // do something with the customer

    return View();
}

这也工作,对我来说。我们正在与一个标准的路线做它在我们的应用程序现在:

This works too, for me. We're doing it in our application right now with a standard route:

public ActionResult Confirm(string id)
{
     if (!string.IsNullOrEmpty(id)) // check the id is actually a valid string
         _customerServer.GetByStringId(id);

    // do something with the customer

    return View();
}

这篇关于正在使用的查询字符串id参数不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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