实施自定义MVC基本视图页面 [英] Implement a Custom MVC Base View Page

查看:90
本文介绍了实施自定义MVC基本视图页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现MVC定制基础视图页面,以覆盖" User属性类型.这将使我的CustomPrincipal类型在任何视图中均可访问.

I am attempting to implement an MVC custom base view page in order to "override" the User property type. This will make my CustomPrincipal type accessible in any view.

搜索网络后,我发现

After searching the web, I found Phil Haack's instructions for implementing a custom base view page. I followed the instructions exactly as they are written, but I ran into an issue while accessing the properties in the view.

当我打开一个视图时,以前的所有HTML帮助器操作都以蓝色的波浪线加下划线.当我将光标放在@Html部分上时,它会显示错误:

When I open a view, any previous Html helper actions are underlined with the blue, squiggly line. When I put my cursor over the @Html portion, it reveals the error:

"HTML是模糊的,是从名称空间或类型'System.Web.WebPages,System.Web.Mvc'导入的."

"Html is ambiguous, imported from the namespaces or types 'System.Web.WebPages, System.Web.Mvc'."

现在,我知道为什么收到此消息,但是我不知道如何解决它.我不知道为什么这很重要,但是当前的应用程序是在Visual Basic中创建的.作为辅助测试,我用C#创建了另一个MVC应用程序,并尝试实现自定义基本视图页面.在C#应用程序中,它工作得很好.我可以从视图中访问我的自定义属性.

Now, I understand why I am receiving the message, but I don't understand how to fix it. I don't know why this would matter, but the current application was created in Visual Basic. As a secondary test, I created another MVC application in C# and attempted to implement the custom base view page. In the C# application it worked just fine. I can access my custom property from within the views.

我已经在网上搜寻了有关此问题的答案,但到目前为止还没有找到任何东西.还有其他人遇到类似的问题吗?

I have scoured the web looking for an answer to this issue, but so far have not found anything. Has anybody else run into a similar issue?

作为参考,我在下面添加了自定义基本视图页面和~/Views/web.config:

For reference, I included my custom base view page and ~/Views/web.config below:

BaseViewPage

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using CCInfoController;

namespace CCInfo.Web.Mvc
{
    public class BaseViewPage<TModel> : WebViewPage<TModel>
    {
        public new CustomPrincipal User
        {
            get
            {
                return base.User as CustomPrincipal;
            }
        }

        public override void Execute()
        {
        }
    }
}

〜/Views/web.config

<configuration>
  <configSections>
    <sectionGroup name="system.web.webPages.razor"
      type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup,
      System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral,
      PublicKeyToken=31BF3856AD364E35">
    <section name="host" 
       type="System.Web.WebPages.Razor.Configuration.HostSection,
       System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral,
       PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    <section name="pages"
       type="System.Web.WebPages.Razor.Configuration.RazorPagesSection,
       System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral,
       PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>

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

推荐答案

您需要提供WebViewPage的两个版本,一个是通用版本,另一个是非通用版本.

You need to provide 2 versions of the WebViewPage, a generic and non-generic.

public class BaseViewPage<TModel> : WebViewPage<TModel>
{
}


public class BaseViewPage : WebViewPage
{
}

这篇关于实施自定义MVC基本视图页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆