【问题标题】:error on query string parameters查询字符串参数错误
【发布时间】:2012-01-09 20:30:40
【问题描述】:

我需要开始处理包含一些电子邮件和 GUID 的 URL。如下所示,第一个参数是电子邮件地址,第二个参数是 Guid。

http://www.myWebSiteurladdress.com/Account/MyActionMethod?MyEmail=me@here.com?MyId=222DF915-264E-4034-BF26-22EB1165667C

为此我修改了我的路由如下

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

    routes.MapRoute(
       "RouteABC", // Route name
       "{controller}/{action}/{mail}/{id}", // URL with parameters
       new { controller = "Account", action = "MyActionMethod", mail = string.Empty, id = Guid.Empty } // Parameter defaults
   );

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

然后我有如下的操作方法。

 public class AccountController : Controller
    {
        public ActionResult MyActionMethod(string email, Guid id)
        {

            ............
        }

问题是当我继续上面的网址时,http://www.myWebSiteurladdress.com/Account/MyActionMethod?MyEmail=me@here.com?MyId=222DF915-264E-4034-BF26-22EB1165667C 我得到以下错误。有什么想法我可能在这里做错了吗?

参数字典包含参数“MyId”的空条目 方法的不可为空类型“System.Guid” 'System.Web.Mvc.ActionResult MyActionMethod(System.String, System.Guid)”在“SmartChartsMVC.Controllers.AccountController”中。一个 可选参数必须是引用类型、可为空的类型,或者是 声明为可选参数。参数名称:参数

【问题讨论】:

    标签: asp.net-mvc-3 routing


    【解决方案1】:

    您可以在格式正确的网址中添加一个问号。它将路径部分与查询字符串分开。因此,您尝试导航到的 url 无效。

    更现实的网址可能如下:

    http://www.myWebSiteurladdress.com/Account/MyActionMethod/me@here.com/222DF915-264E-4034-BF26-22EB1165667C
    

    还要确保您的路由名称令牌与您的操作参数匹配。在您的 Global.asax 路由定义中,您使用了 {mail} 而在您的操作参数中,您使用 email 作为参数的名称。确保您的命名约定保持一致。

    如果你只是想要一个这样的网址:

    http://www.myWebSiteurladdress.com/Account/MyActionMethod?MyEmail=me@here.com&MyId=222DF915-264E-4034-BF26-22EB1165667C
    

    那么您不需要添加任何自定义路由,因为默认路由足以调用以下操作:

    public class AccountController : Controller
    {
        public ActionResult MyActionMethod(string myEmail, Guid myId)
        {
            ...
        }
    
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2015-10-15
      • 1970-01-01
      • 2019-08-04
      相关资源
      最近更新 更多