简体   繁体   中英

mvc action method doesn't recognize parameter

I'm trying to set membership provider with confirmation email. User is successfully registrated using memb. provider.

After registration, a confirmation email is being sent with userProviderKey which is used to approve user. Link is sent as follows

http://localhost:48992/Account/Verify/e37df60d-b436-4b19-ac73-4343272e10e8

User must click on link sent with key (providerUserKey), problem that is this key does not even show in debug mode as parameter

// in debug providerUserKey is null
public ActionResult Verify(string providerUserKey)
{    
}

What can be the problem?

Unless you've specifed a rule in your global asax your url should look like

http://localhost:48992/Account/Verify?providerUserKey=e37df60d-b436-4b19-ac73-4343272e10e8

if you want to use the above format you need to map a new route in your global.asax

routes.MapRoute(
    // Route name
    "routename",
    // Url with parameters
    "Account/Verify/{providerUserKey}/",
    // Parameter defaults
    new { controller = "Account", action = "Verify" }
    );

Try changing the query to

http://localhost:48992/Account/Verify?providerUserKey=e37df60d-b436-4b19-ac73-4343272e10e8

Your ActionResult is looking for this parameter in the URL

The Default MVC Route is {Controller}/{Action}/{Id} Hence it recognize it if the parameter name is Id...

Change it to following and it will work.

public ActionResult Verify(string id)
        {    
        }

Still if you don't want to change the parameter name then you can add following routing in your global.asax file and it will work correctly.

routes.MapRoute("MyRouteName","{Controller}/{action}/{providerUserKey}")

You can also pass the default values to it if needed as your path is predefined. Cheers

If you have the route configuration of Global.asax.cs file as :

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

You can see it has id parameter as optional. So if the querystring is recognized as id then it will have the value that you have passed.

Change the id to providerUserKey inorder to do the job for you.

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

Or Keep the default optional parameter as id and pass providerUserKey as additional querystring parameter.

Say like this:

     @Html.Action("","",new { providerUserKey="e37df60d-b436-" })

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM