2

我怀疑某处有一些隐藏的魔法阻止了 T4MVC 中所有看起来像实际的方法调用。然后我的视图编译失败,stackTrace 进入了我的实际方法。

        [Authorize]
    public string Apply(string shortName)
    {
        if (shortName.IsNullOrEmpty())
            return "Failed alliance name was not transmitted";
        if (Request.IsAuthenticated == false || User == null || User.Identity == null)
            return "Apply authentication failed";
        Models.Persistence.AlliancePersistance.Apply(User.Identity.Name, shortName);
        return "Applied";
    }

所以这个方法毕竟不是在模板中生成的。

<%=Ajax.ActionLink("Apply", "Apply", new RouteValueDictionary() { { "shortName", item.Shortname } }, new AjaxOptions() { UpdateTargetId = "masterstatus" })%>

            <%=Html.ActionLink("Apply",MVC.Alliance.Apply(item.Shortname),new AjaxOptions() { UpdateTargetId = "masterstatus" }) %>

第二种方法在编译时引发了异常,因为Apply我的控制器中的方法具有一个[Authorize]属性,因此如果未登录的人单击此属性,他们将被重定向到登录,然后直接返回此页面。在那里他们可以再次单击应用,这次是登录。

是的,我意识到一个是Ajax.ActionLink,另一个是Html.ActionLink我确实在 T4MVC 版本中尝试过它们。

4

1 回答 1

2

更新:我看到了问题。T4MVC 仅支持返回 ActionResult 的操作,因此它不处理返回字符串的特定操作。您可以通过如下更改来解决此问题:

    [Authorize]
    public ActionResult Apply(string shortName) {
        if (shortName.IsNullOrEmpty())
            return Content("Failed alliance name was not transmitted");
        if (Request.IsAuthenticated == false || User == null || User.Identity == null)
            return Content("Apply authentication failed");
        Models.Persistence.AlliancePersistance.Apply(User.Identity.Name, shortName);
        return Content("Applied");
    }

请注意它如何返回 ActionResult,并调用 'return Content("...")' 而不是直接返回字符串。


您能否提供有关您得到的编译异常的更多详细信息?我认为这是您在浏览器中看到的,而不是在 VS 中看到的?你能包括错误的全文吗?

通常,通过 MVC 前缀的 T4MVC 调用绝不应该调用实际的操作方法。相反,它们调用派生类中的重写方法。查找名为 AllianceController.generated.cs 的生成文件(在 T4MVC.tt 下)。您应该在那里看到一个被覆盖的“应用”方法,它正好满足 T4MVC 的需要。

于 2010-04-10T05:38:36.490 回答