Reputation: 355
After reading a few other threads it seemed like we needed to map a new route in Global.asax.cs in order to be pass multiple variables. (note: it works fine if just passing one variable)
However if passing multiple variables the first one goes through but the second one (and any other ones after that) do not pass through -- their value is null.
As an example the url value in _Layout.cshtml is var url = '/Home/DidItWork?testUserName=James%20Bond&testEmail=james.bond%40gmail.com';
-- should that be coming out differently since the MapRoute is expecting something in the form of Home/DidItWork/{testUserName}/{testEmail}
?
Thoughts or suggestions for why it's not passing multiple variables?
Global.asax.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"MyNewRoute", // Route name
"Home/DidItWork/{testUserName}/{testEmail}", // URL with parameters
new { controller = "Home", action = "DidItWork" } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
HomeController.cs
public async Task<ActionResult> DidItWork(string testUserName, string testEmail)
{
var manager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
var user = new ApplicationUser { UserName = "user name", Email = "[email protected]" };
var result = await manager.CreateAsync(user, "passwordGoesHere1!");
if (result.Succeeded)
{
await manager.SendEmailAsync(user.Id, testUserName, testEmail);
return View();
}
return View();
}
_Layout.cshtml
@*script for someone who presses the send/email button*@
<script>
$("#sendButton").click(function () {
var htmlBody = $("#myModal .modal-body").text();
var url = '@Url.Action("DidItWork", "Home", new { testUserName = "James Bond", testEmail = "[email protected]" })';
$.ajax({
url: url,
success: function (result) {
alert("Success. It worked." + htmlBody);
},
error: function (xhr, status, errorThrown) {
alert("Sorry, there was a problem! Error: " + errorThrown + ". Status: " + status + ". Console: " + xhr);
console.log("Hello");
console.log("Error: " + errorThrown);
console.log("Status: " + status);
console.dir(xhr);
console.log("Good-bye");
},
});
return false;
});
</script>
Upvotes: 1
Views: 4393
Reputation: 56869
I suspect it is an encoding issue. There are 2 ways to encode a URL:
/Home/DidItWork?testUserName=James%20Bond&testEmail=james.bond%40gmail.com
/Home/DidItWork?testUserName=James%20Bond&testEmail=james.bond%40gmail.com
When you pass a URL to a web server, it should be URL encoded, otherwise the server won't be able to interpret the &
sign in the query string.
This would explain why it is working with one variable, but not two.
Update
To create a URL without encoding for use with JavaScript, you need to pass the output of Url.Action
to Html.Raw
.
var url = '@Html.Raw(Url.Action("DidItWork", "Home", new { testUserName = "James Bond", testEmail = "[email protected]" }))';
Upvotes: 8