c# - 为什么我的 ASP.NET 操作寻找错误的 View ?

标签 c# asp.net-mvc asp.net-mvc-2

我有一个简单的 Action :

    public ActionResult CommentError(string error)
    {
        return View(error);
    }

我有一个名为 CommentError.ascx 的简单局部 View :

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<String>" %>

<%: Model %>

当我通过转到 myurl.com/find/Comments/CommentError 浏览到 View 目录时, View 显示正常...没有错误。

但是,当我转到 myurl.com/find/Comments/CommentError?error=SomeErrorString 时,它没有将查询字符串绑定(bind)到 string error,而是寻找一个名为 SomeErrorString.ascx 的 View 。

为什么会这样?

编辑
请注意,我确实有一个自定义的 global.asax,如我正在使用的路径所示(/find/Comments/CommentError:::/find/{controler}/{ Action })

最佳答案

如前所述,MVC 正在寻找与字符串参数同名的 View 。为避免这种情况,您需要将其转换为一个对象...

public ActionResult CommentError(string error)
{
    return View((object)error);
}

关于c# - 为什么我的 ASP.NET 操作寻找错误的 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4522409/

相关文章:

c# - 将外键添加到其他表或在选择查询中创建平面表的速度更快

asp.net - 如何将 DynamicData 路由与 Mvc 路由混合使用?

c# - 使用托管页面的 Express Checkout 和信用卡付款的 Paypal Advanced 定期计费

c# - 如何获取要在 MVC 2 中显示的位图图像

c# - 在操作结果中将 EditorTemplate 作为 PartialView 返回

asp.net - 带有 <httpErrors> 元素的自定义 404 页面似乎在 IIS 7 中不起作用

c# - Metro - 编写异步 c# 操作并从 javascript 调用

c# - Azure Signalr +Azure function + wpf 客户端 - 如何使用 azure function 和自托管 Azure SignalR App 拥有 wpf 客户端应用程序

c# - .NET 中真正不可变的字典

asp.net-mvc - 从 Mvc 3 View 填充 List<Objects>