问题
I have a simple form with just one textbox and one submit button. The form basically sends to a different page with the value in the textbox as querystring. When I click on the submit button, the querystring is in this format, for example:
mysite.com/?TargetCode=Test1
I would like it to display in this format: mysite.com/Test1
I already have an Action in my HomeController
that take the "TargetCode" as the querystring, and I've already setup a routing in the Global.ascx.cs
for that. What should I do to re-write the querystring so it doesn't have that "?TargetCode=" in the URL? Here is the code for the form I have:
<% using (Html.BeginForm("Index", "Home", FormMethod.Get, new { id = "CodeForm" }))
回答1:
I think you'd have to use jQuery to do the submission otherwise you're dependent on how the browser constructs the URL. Updated to include a hook into the validation plugin.
$('#CodeForm').submit( function() {
var $form = $(this);
if ($form.valid()) {
window.location.href = $form.attr('action')
+ '/'
+ $('[name=TargetCode]').val();
}
return false;
});
来源:https://stackoverflow.com/questions/1574181/asp-net-mvc-rewriting-formmethod-get-querystring