c# – 单声道Mac上的ASP.net MVC Razor视图

我使用Visual Studio Community Edition for Mac创建了一个ASP.net MVC应用程序.当我运行默认的HomeController和索引视图工作正常.然后我做了一个BookController并在里面的视图中添加了一个文件夹Book和Index.cshtml文件.但是我一直收到以下错误:

System.InvalidOperationException The view found at
‘~/Views/book/Index.cshtml’ was not created.

描述:

HTTP 500.Error processing request.

细节:

Non-web exception. Exception origin (name of application or object):
System.Web.Mvc. Exception stack trace: at
System.Web.Mvc.BuildManagerCompiledView.Render
(System.Web.Mvc.ViewContext viewContext,System.IO.TextWriter writer)
[0x00061] in :0 at
System.Web.Mvc.ViewResultBase.ExecuteResult
(System.Web.Mvc.ControllerContext context) [0x00080] in
:0 at
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult
(System.Web.Mvc.ControllerContext controllerContext,
System.Web.Mvc.ActionResult actionResult) [0x00000] in
:0 at
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive
(System.Collections.Generic.IList1[T] filters,System.Int32
filterIndex,System.Web.Mvc.ResultExecutingContext preContext,
System.Web.Mvc.ControllerContext controllerContext,
System.Web.Mvc.ActionResult actionResult) [0x0000b] in
<cc73190bab9d435c831510ff295c572a>:0 at
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive
(System.Collections.Generic.IList
1[T] filters,
System.Web.Mvc.ActionResult actionResult) [0x0009b] in
:0 at
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters
(System.Web.Mvc.ControllerContext controllerContext,
System.Collections.Generic.IList1[T] filters,
System.Web.Mvc.ActionResult actionResult) [0x0000a] in
<cc73190bab9d435c831510ff295c572a>:0 at
System.Web.Mvc.Async.AsyncControllerActionInvoker+<>c__DisplayClass21+<>c__DisplayClass2b.<BeginInvokeAction>b__1c
() [0x0008a] in <cc73190bab9d435c831510ff295c572a>:0 at
System.Web.Mvc.Async.AsyncControllerActionInvoker+<>c__DisplayClass21.<BeginInvokeAction>b__1e
(System.IAsyncResult asyncResult) [0x00041] in
<cc73190bab9d435c831510ff295c572a>:0 at
System.Web.Mvc.Async.AsyncResultWrapper+WrappedAsyncResult
1[TResult].CallEndDelegate
(System.IAsyncResult asyncResult) [0x00000] in
:0 at
System.Web.Mvc.Async.AsyncResultWrapper+WrappedAsyncResultBase1[TResult].End
() [0x00029] in <cc73190bab9d435c831510ff295c572a>:0 at
System.Web.Mvc.Async.AsyncResultWrapper.End[TResult]
(System.IAsyncResult asyncResult,System.Object tag) [0x00007] in
<cc73190bab9d435c831510ff295c572a>:0 at
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction
(System.IAsyncResult asyncResult) [0x00000] in
<cc73190bab9d435c831510ff295c572a>:0 at
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d (System.IAsyncResult
asyncResult,System.Web.Mvc.Controller+ExecuteCoreState innerState)
[0x00000] in <cc73190bab9d435c831510ff295c572a>:0 at (wrapper
delegate-invoke)
System.Web.Mvc.Async.EndInvokeVoidDelegate
1[System.Web.Mvc.Controller+ExecuteCoreState]:invoke_void_IAsyncResult_TState
(System.IAsyncResult,System.Web.Mvc.Controller/ExecuteCoreState) at
System.Web.Mvc.Async.AsyncResultWrapper+WrappedAsyncVoid1[TState].CallEndDelegate
(System.IAsyncResult asyncResult) [0x00000] in
<cc73190bab9d435c831510ff295c572a>:0 at
System.Web.Mvc.Async.AsyncResultWrapper+WrappedAsyncResultBase
1[TResult].End
() [0x00029] in :0 at
System.Web.Mvc.Async.AsyncResultWrapper.End[TResult]
(System.IAsyncResult asyncResult,System.Object tag) [0x00007] in
:0 at
System.Web.Mvc.Async.AsyncResultWrapper.End (System.IAsyncResult
asyncResult,System.Object tag) [0x00000] in
:0 at
System.Web.Mvc.Controller.EndExecuteCore (System.IAsyncResult
asyncResult) [0x00000] in :0 at
System.Web.Mvc.Controller.b__15 (System.IAsyncResult
asyncResult,System.Web.Mvc.Controller controller) [0x00000] in
:0 at
System.Web.Mvc.Async.AsyncResultWrapper+WrappedAsyncVoid1[TState].CallEndDelegate
(System.IAsyncResult asyncResult) [0x00000] in
<cc73190bab9d435c831510ff295c572a>:0 at
System.Web.Mvc.Async.AsyncResultWrapper+WrappedAsyncResultBase
1[TResult].End
() [0x00029] in :0 at
System.Web.Mvc.Async.AsyncResultWrapper.End[TResult]
(System.IAsyncResult asyncResult,System.Object tag) [0x00000] in
:0 at
System.Web.Mvc.Controller.EndExecute (System.IAsyncResult asyncResult)
[0x00000] in :0 at
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute
(System.IAsyncResult asyncResult) [0x00000] in
:0 at
System.Web.Mvc.MvcHandler.b__5
(System.IAsyncResult asyncResult,
System.Web.Mvc.MvcHandler+ProcessRequestState innerState) [0x00000] in
:0 at (wrapper delegate-invoke)
System.Web.Mvc.Async.EndInvokeVoidDelegate1[System.Web.Mvc.MvcHandler+ProcessRequestState]:invoke_void_IAsyncResult_TState
(System.IAsyncResult,System.Web.Mvc.MvcHandler/ProcessRequestState)
at
System.Web.Mvc.Async.AsyncResultWrapper+WrappedAsyncVoid
1[TState].CallEndDelegate
(System.IAsyncResult asyncResult) [0x00000] in
:0 at
System.Web.Mvc.Async.AsyncResultWrapper+WrappedAsyncResultBase`1[TResult].End
() [0x00029] in :0 at
System.Web.Mvc.Async.AsyncResultWrapper.End[TResult]
(System.IAsyncResult asyncResult,System.Object tag) [0x00000] in
:0 at
System.Web.Mvc.MvcHandler.EndProcessRequest (System.IAsyncResult
asyncResult) [0x00000] in :0 at
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest
(System.IAsyncResult result) [0x00000] in
:0 at
System.Web.HttpApplication.async_handler_complete_cb
(System.IAsyncResult ar) [0x00015] in
/private/tmp/source-mono-2017-02/bockbuild-2017-02/profiles/mono-mac-xamarin/build-root/mono-x86/mcs/class

我用Google搜索,人们说我必须确保views文件夹中的web.config如下:

<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory,System.Web.Mvc,Version=5.2.3.0,Culture=neutral,PublicKeyToken=31BF3856AD364E35" />

这是它与system.web.mvc的版本匹配,并查看system.web.mvc内部的版本是:

5.2.3

HomeController中的默认视图有效.我是第一次在Mac上试用ASP.net MVC而且在Windows上从未遇到过这种类型的问题.

欢迎任何帮助.

更新控制器代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TestMvcFromMac.Models;

namespace TestMvcFromMac.Controllers
{
 public class BookController : Controller
 {

    //Category category = new Category();


    public ActionResult Index()
    {
        return View ();
    }


 }
}

解决方法

我猜测问题可能会发生,因为你手动创建所有的东西,而不是让模板引擎为你生成一些东西,如在Windows中的visual studio(个人没有尝试Mac版本所以我不知道它究竟是怎么样的).无论如何,假设您已经创建了控制器,然后创建了Book文件夹.但是如果没有配置,还有手动配置的东西,那就是当前控制器的路由.

你必须在RouteConfig文件类中设置路由逻辑,所以你会有这样的东西:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",url: "{controller}/{action}/{id}",defaults: new { controller = "Book",action = "Index",id = UrlParameter.Optional }
        );

有关详细信息,请参阅此问题here.

我猜的另一个版本是在return语句中设置视图名称,如下所示:

return View("Index")

如果不是这种情况,请告诉我,以便我可以看到此问题的任何其他替代方案……

相关文章

项目中经常遇到CSV文件的读写需求,其中的难点主要是CSV文件...
简介 本文的初衷是希望帮助那些有其它平台视觉算法开发经验的...
这篇文章主要简单记录一下C#项目的dll文件管理方法,以便后期...
在C#中的使用JSON序列化及反序列化时,推荐使用Json.NET——...
事件总线是对发布-订阅模式的一种实现,是一种集中式事件处理...
通用翻译API的HTTPS 地址为https://fanyi-api.baidu.com/api...