0

我正在尝试这样做:

HttpContext.GetLocalResourceObject("~/Views/Shared/AnotherFolder/More/App_LocalResources/MyResourceFile.cshtml.resx", "myKey") 

但我不断从控制器内部收到此错误:

The resource class for this page was not found. Please check if the resource file exists and try again.

那就是如果我将调用包装在 try 块中。否则,该函数将被弹出。

目录结构,Views就在根文件夹之后:

目录结构

我确定路径是正确的,因为我刚刚从资源文件的属性窗口中复制了它,但可能有什么问题?虚拟路径不正确?

这是调试输出:

A first chance exception of type 'System.InvalidOperationException' occurred in System.Web.dll
System.Data.SqlClient.SqlException (0x80131904): Could not find stored procedure 'ELMAH_LogError'.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at Elmah.SqlErrorLog.Log(Error error)
   at Elmah.ErrorLogModule.LogException(Exception e, HttpContext context)
4

1 回答 1

0

这篇MSDN文章指出:

存储在特殊 App_LocalResources 文件夹中的本地默认资源文件根据 ASP.NET 页面命名。例如,如果在 Default.aspx 页中使用以下代码,则资源文件必须命名为 Default.aspx.resx。

你还没有说你从哪里(或确切地)打电话:

HttpContext.GetLocalResourceObject("~/Views/Shared/AnotherFolder/More/App_LocalResources/MyResourceFile.cshtml.resx", "myKey")

因此,正如文章所建议的,如果您从名为“Default.aspx”的文件中调用此方法(例如),请将资源文件重命名为“Default.aspx.resx”并改用此方法调用:

var resource = GetLocalResourceObject("myKey").ToString();

请注意,我已将结果分配给“资源”,因为我假设您在实际代码中执行类似的操作,还请注意需要将结果转换为您期望的任何类型。

于 2013-10-02T21:20:28.483 回答