palm snow
palm snow

Reputation: 2392

error on query string parameters

I need to start processing a URL that will contain some emailand a GUID. Some thing like following where first param is email address and second param is a Guid.

http://www.myWebSiteurladdress.com/Account/[email protected]?MyId=222DF915-264E-4034-BF26-22EB1165667C

For this I have modified my routing as below

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
    );
}

Then I have my action method like below.

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

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

Problem is when I go on the above url, http://www.myWebSiteurladdress.com/Account/[email protected]?MyId=222DF915-264E-4034-BF26-22EB1165667C I get following error. Any ideas what I may be doing wrong here?

The parameters dictionary contains a null entry for parameter 'MyId' of non-nullable type 'System.Guid' for method 'System.Web.Mvc.ActionResult MyActionMethod(System.String, System.Guid)' in 'SmartChartsMVC.Controllers.AccountController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

Upvotes: 0

Views: 712

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

You could have a single question mark in a properly formatted url. It is what separates the path portion from the query string. So the url you are trying to navigate to is not valid.

A far more realistic url could be the following:

http://www.myWebSiteurladdress.com/Account/MyActionMethod/[email protected]/222DF915-264E-4034-BF26-22EB1165667C

Also make sure that your route name token matches your action parameter. In your Global.asax route definition you used {mail} whereas in your action argument you use email as name of the parameter. Make sure you are consistent in your naming convention.

And if you simply wanted to have an url like this:

http://www.myWebSiteurladdress.com/Account/[email protected]&MyId=222DF915-264E-4034-BF26-22EB1165667C

then you don't need to add any custom routes as the default route will be sufficient to call the following action:

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

    ...
}

Upvotes: 1

Related Questions