Why is using a URL containing a questionmark when

2019-06-06 14:37发布

问题:

I'm using MVC3 and in my view I have the following code block;

Response.Redirect(@Url.Action("index","home", new {error = "test"}));

so the url i would expect is http://marketurk.co.uk/?error=test but I get is this http://marketurk.co.uk/%3Ferror=test so that I get potentially dangerous request.

does anyone have the same issue, and could tell me what I'm doing wrong?

UPDATE: I moved my logic into a controller method as follows;

public ActionResult Index()
    {

        if (!User.Identity.IsAuthenticated)
            return RedirectToAction("index", "home", new { error = "test" });

        return View();
    }   

It still redirects to http://marketurk.co.uk/%3Ferror=test

回答1:

This is typically logic which would go into the controller I'd say. There's the ever present unwritten rule that your view should contain as little logic as possible, unless it's intended for presentation purposes.

In your controller action you could verify your view model, and if it's invalid you should use the built-in RedirectToAction:

public ActionResult SomeAction()
{
    // build your viewmodel here //

    if(/* viewmodel is invalid */)
        return RedirectToAction("index", "Home", new { error = "test" });
    else
        return View( /* your viewmodel */);
}


回答2:

Answer should be something:
as ? encoded to %3F, by using Html.Raw you can keep ?

Response.Redirect(Html.Raw(Url.Action("index","home", new {error = "test"})));


回答3:

are you using an OnActionExecuting function for that Controller? Maybe problem caused by another line of code after your request?