【问题标题】:AllowHtml attribute not working on productionAllowHtml 属性不适用于生产
【发布时间】:2013-10-16 21:56:56
【问题描述】:

我有一个模型需要捕获 html。我已将 [AllowHtml] 属性添加到模型属性中,并且在调试时它可以在我的本地服务器上正常工作。

然而,一旦部署到生产环境,它在生产服务器上执行时可以正常工作(即我远程到服务器并在那里浏览它),但在从任何其他服务器执行时会失败并显示通常的“潜在危险等等等等”消息机器。

所以在我看来,这与验证所涉及的位置有关,还是我完全错过了这条船。

确认一下,我没有对 web.config 进行任何“特殊”更改。

请有人解释我为什么会遇到这个问题。

型号

[AllowHtml]
[Display(Name = "Overview")]
public string Overview { get; set; }

控制器

//
// POST: /Product/
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EditFeature(BackOffice.Models.ProductFeature model)
{
        if (ModelState.IsValid)
        {
            //insert the new product
        }
        //invalid model, return with errors
        return View(model);
 }

查看

@model BackOffice.Models.ProductFeature

@using (Html.BeginForm("AddFeature", "Product", null, FormMethod.Post, new { role = "form", @class = "form-horizontal" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    @Html.Hidden("ProductID", @Model.ProductID)

    <div class="modal fade" id="FeatureModal" tabindex="-1" role="dialog" aria-labelledby="FeatureModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title">Add a Feature</h4>
                </div>
                <div class="modal-body">
                    <div class='form-group'>
                        <label class='col-lg-2 control-label'>Title</label>
                        <div class="col-lg-10">
                            @Html.TextBoxFor(m => m.Title, new { @class = "form-control" })
                            @Html.ValidationMessageFor(m => m.Title)
                        </div>

                    </div>
                    <div class='form-group'>
                        <label class='col-lg-2 control-label'>Overview</label>
                        <div class="col-lg-10">
                            @Html.TextAreaFor(m => m.Description, 10, 40, new { @class = "ckeditor", id = "overview" })
                        </div>

                    </div>
                </div>
                <div class='clearfix'></div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary">Add</button>
                </div>
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </div>
    <!-- /.modal -->
}

【问题讨论】:

  • EDIT和ADD使用的模型是一样的吗?
  • @Romias 是的,它们是相同的,当从不同的机器调用时,它们都在插入/更新时失败。

标签: c#-4.0 asp.net-mvc-4 attributes


【解决方案1】:

这里的方法名称不匹配。你有

@using (Html.BeginForm("AddFeature", "Product", null, FormMethod.Post, new { role = "form", @class = "form-horizontal" }))
{
}

但是你的动作方法被调用了

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EditFeature(BackOffice.Models.ProductFeature model)
{
}

AddFeature 操作方法在哪里?

【讨论】:

    猜你喜欢
    • 2011-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    相关资源
    最近更新 更多