【问题标题】:Asp.NET MVC3 Bad RequestAsp.NET MVC3 错误请求
【发布时间】:2012-12-03 18:54:53
【问题描述】:

这是我在 ASP.NET MVC (MVC3) 中的第一个项目,我在处理所有事情时遇到了很多麻烦。根据选择的语言,我在页面中的所有文本都将从数据库中选择。对于这种语言选择,我更喜欢使用 Session 变量。我需要一个语言的图片链接,所以我将以下行写入 .cshtml 页面。

@using (Html.BeginForm()) {
    <a href='<%: Url.Action("Index", "Home", new { lang="Tr" }) %>'>
        <img src="../../Content/Images/flag_tr.jpg" width="40" height="20" />
    </a>
    <a href='<%: Url.Action("Index", "Home", new { lang = "En" }) %>'>
        <img src="../../Content/Images/flag_en.jpg" width="40" height="20" />
    </a>
}

在HomeController中:

    public ActionResult Index()

        ViewBag.Message = "Welcome to ASP.NET MVC!";
        ViewBag.Selected = "Not yet selected";
        return View();
    }

    [HttpPost]
    public ActionResult Index(String lang)
    {
        if (lang == "Tr")
        {
            System.Web.HttpContext.Current.Session["Language"] = "Tr";
        }
        else if (lang == "En")
        {
            System.Web.HttpContext.Current.Session["Language"] = "En";
        }
        ViewBag.Selected = System.Web.HttpContext.Current.Session["Language"];
        return View();
    }

当我单击标志链接时,我收到“HTTP 错误 400 - 错误请求”。谁能告诉我我做错了什么,或者我应该怎么做?

P.S.:我也试过不使用 Form,在 Controller 中添加一个名为 Lang 的新函数并从那里重定向到 Index,但没有成功。

【问题讨论】:

    标签: asp.net-mvc session session-variables


    【解决方案1】:

    您似乎在混合使用 Razor 和 WebForms 语法。当您在页面上“查看源代码”时,我相信您不会在锚点中看到正确的 URL:

    <a href='<%: Url.Action("Index", "Home", new { lang="Tr" }) %>'>
    

    应该是:

    <a href='@Url.Action("Index", "Home", new { lang="Tr" })'>
    

    另外,请注意,即使在表单中,锚也会导致 HTTPGET,因此您需要使用 JavaScript 覆盖它们的行为,或者将 lang 检查添加到控制器操作的 HTTPGET 版本。尝试这样的事情,将您的两个控制器操作合二为一:

    public ActionResult Index(string lang)
    {
        if (lang == "Tr")
        {
            System.Web.HttpContext.Current.Session["Language"] = "Tr";
        }
        else if (lang == "En")
        {
            System.Web.HttpContext.Current.Session["Language"] = "En";
        }
    
        ViewBag.Message = "Welcome to ASP.NET MVC!";
        ViewBag.Selected = System.Web.HttpContext.Current.Session["Language"] ?? "Not yet selected";
        return View();
    }
    

    如果你不使用[HttpGet] 或[HttpPost] 来装饰它,那么任何HTTP 动词都会映射到这个动作。

    【讨论】:

    • 非常感谢!关于 HttpGet;我放置了 HttpGet 而不是 HttpPost,我可以得到 Tr 和 Eng 作为输出,但我现在没有得到原始的 Index() 功能。抱歉我缺乏知识,我对 C# 很陌生,我可以在这里假设 [HttpGet] 是 Java 中 @Override 的一种形式吗?
    • 查看我的编辑以获取示例控制器操作。我还会编辑您的问题的标题,因为它与 Session 无关。
    【解决方案2】:

    如果我没记错的话,你的 HomeController 类名是 HomeController。

    <a href='<%: Url.Action("Index", "HomeController", new { lang="Tr" }) %>'>
        <img src="../../Content/Images/flag_tr.jpg" width="40" height="20" />
    </a>
    

    所以当你在 Url.Action 中指定 Controller 时,它应该只是 Home 而不是 HomeController。

    因此您的代码将如下所示:

    @using (Html.BeginForm()) {
        <a href='<%: Url.Action("Index", "Home", new { lang="Tr" }) %>'>
            <img src="../../Content/Images/flag_tr.jpg" width="40" height="20" />
        </a>
        <a href='<%: Url.Action("Index", "Home", new { lang = "En" }) %>'>
            <img src="../../Content/Images/flag_en.jpg" width="40" height="20" />
        </a>
    }
    

    让我知道这是否适合您,或者您需要更多说明。谢谢

    【讨论】:

    • 谢谢,但我在回答之前识别/编辑了问题。
    • 这甚至不是有效的 Razor 或 WebForms 语法,而是一种合并。
    猜你喜欢
    • 2011-03-09
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 2013-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-28
    相关资源
    最近更新 更多