为什么使用重定向时获得一个“有潜在危险的请求”包含一个问号的URL? [英] Why is using a URL containing a questionmark when redirecting gets a “potentially dangerous request”?

查看:205
本文介绍了为什么使用重定向时获得一个“有潜在危险的请求”包含一个问号的URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MVC3在我看来,我有以下的code座;

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

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

所以我期望的URL为 http://marketurk.co.uk/?error=test 但我得到的是这种 http://marketurk.co.uk/%3Ferror=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();
    }   

它仍然重定向到的 http://marketurk.co.uk/%3Ferror=test

推荐答案

这是典型的逻辑,会去到控制器我说。还有,你的看法应该包含尽可能少的逻辑可能,除非它适用于presentation用途日益present不成文的规定。

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.

在你的控制器动作,你可以验证您的视图模型,如果它是无效的,你应该使用内置的RedirectToAction:

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

这篇关于为什么使用重定向时获得一个“有潜在危险的请求”包含一个问号的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆