ASP.NET MVC3 Razor - 将视图放置在备用位置时丢失智能感知?

发布于 2024-10-16 14:31:46 字数 1709 浏览 2 评论 0原文

VS2010 Ultimate、ASP.NET MVC 3 带 Razor。

我在我的 MVC3 应用程序中创建了一个自定义视图引擎,允许嵌套区域,例如

~/areas/admin
    /marketing
        /views
            index       
        /controllers
            marketingController
        /email
            /views
                index
                ...
            /controllers
                emailController
        /templates
            /views
                index
                edit
                ...                  
            /controllers
                templatesControler

等。

这一切都很好,除了我似乎在不在标准 ~/areas/ 中的视图中丢失了智能感知。 area_name/views/myview.cshtml 位置。

有什么建议吗?

更新

只是一时兴起,我添加了一条 @inherits 语句

@inherits System.Web.Mvc.WebViewPage<Namespace.Models.Class>

,然后智能感知开始工作。然后我删除了该语句,它继续工作。

除了文件扩展名之外,项目文件中是否有某些设置或告诉 Visual Studio 对打开的文件应用哪种智能感知的设置? (如果仅使用扩展名,我希望它会更加一致)。

更新 2

虽然将 web.config 添加到我的每个视图文件夹可以很好地解决问题,但将 razor 配置放在根 web.config 中却不能。

将所需的 system.web.webPages.razor 部分添加到 ~/web.config 后,

  1. 我将 Razor .cshtml 视图文件添加到我的嵌套视图文件夹之一。
  2. 智能感知有效。
  3. 我重命名该文件(保留 .cshtml 扩展名)
  4. 智能感知和语法突出显示停止工作。
  5. 我关闭重命名的文件并重新打开它 - 一切都重新开始工作。
  6. 或者,我没有关闭并重新打开文件,而是将其重命名回原来的名称,它再次开始工作。

这感觉就像一个 VS bug——据我所知,重命名文件(但保留正确的扩展名)不应该导致这种行为。我将继续将单独的 web.config 放入每个视图文件夹中,因为它可以解决问题,但是当我希望这些设置通过解决方案树与其他 web.config 设置一样。

还值得注意的是,无论 Razor 配置位于何处,无论智能感知是否正常工作,该站点都会继续正常运行。

已解决

权力机构通知我(速度非常快),这是一个已知问题,已在 MVC3 RTM 和 VS2010 SP1 中得到纠正。更新我的 MVC3 安装并添加 SP1 后,该问题消失了。

VS2010 Ultimate, ASP.NET MVC 3 w/Razor.

I've created a custom view engine in my MVC3 app that allows nested areas, like so

~/areas/admin
    /marketing
        /views
            index       
        /controllers
            marketingController
        /email
            /views
                index
                ...
            /controllers
                emailController
        /templates
            /views
                index
                edit
                ...                  
            /controllers
                templatesControler

etc.

This all works great, except for I seem to have lost intellisense in views that aren't in the standard ~/areas/area_name/views/myview.cshtml location.

Any suggestions?

Update

Just on a lark, I added an @inherits statement

@inherits System.Web.Mvc.WebViewPage<Namespace.Models.Class>

and intellisense started working. I then deleted the statement, and it continues to work.

Is there some setting in the project file or something that tells Visual Studio what sort of intellisense to apply to an open file, other than the file extension? (If the extension was all that was used, I'd expect it to be a whole lot more consistent).

Update 2

While adding a web.config to each of my view folders fixes the problem quite nicely, putting the razor config in the root web.config doesn't.

After adding the required system.web.webPages.razor section to ~/web.config,

  1. I add a Razor .cshtml view file to one of my nested view folders.
  2. Intellisense works.
  3. I rename the file (keeping the .cshtml extension)
  4. Intellesense and syntax highlighting stop working.
  5. I close the renamed file and reopen it - everything starts working again.
  6. Or, instead of closing and reopening the file, I rename it back to its original name, it starts working again.

This feels like a VS bug - renaming a file (but keeping the proper extension) shouldn't, as far as I can tell, cause this sort of behavior. I'll continue putting individual web.configs in each of my view folders since it corrects the problem, but it's annoying to have to clutter up the solution when I'd expect these settings to propagate through the solution tree as other web.config settings do.

It's also worth noting that, no matter where the Razor config is located, the site continues to function correctly whether or not intellesense is working.

Solved

I was informed (incredibly quickly) by the Powers That Be that this was a known issue that is corrected in MVC3 RTM and VS2010 SP1. The issue disappeared after updating my MVC3 installation and adding SP1.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

睡美人的小仙女 2024-10-23 14:31:46

Views 文件夹包含 Web.config 文件它为 Razor 视图设置默认选项,包括默认的基本类型和命名空间,以及启用 @model 指令的自定义主机。

不同文件夹中的 Razor 视图不会继承此配置。
您需要将此配置添加到文件夹中的 Web.config 中:

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
        <namespaces>
            <add namespace="System.Web.Mvc" />
            <add namespace="System.Web.Mvc.Ajax" />
            <add namespace="System.Web.Mvc.Html" />
            <add namespace="System.Web.Routing" />
        </namespaces>
    </pages>
</system.web.webPages.razor>

The Views folder contains a Web.config file that sets default options for Razor views, including the default base type and namespaces, as well as a custom host that enables the @model directive.

Razor views in a different folder won't inherit this configuration.
You need to add this configuration to a Web.config in your folder:

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
        <namespaces>
            <add namespace="System.Web.Mvc" />
            <add namespace="System.Web.Mvc.Ajax" />
            <add namespace="System.Web.Mvc.Html" />
            <add namespace="System.Web.Routing" />
        </namespaces>
    </pages>
</system.web.webPages.razor>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文