VB.NET和MVC 5使用自定义佣工内置佣工和无APP_ code [英] VB.NET and MVC 5 using custom helpers with in-built helpers and no App_Code

查看:271
本文介绍了VB.NET和MVC 5使用自定义佣工内置佣工和无APP_ code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用VB.NET和EF一个MVC 5项目上工作6我还添加了一个EditorTemplates的NuGet包,它依靠自定义全局助手。

I'm working on an MVC 5 project using VB.NET and EF 6. I have added an EditorTemplates nuget package which relies upon Custom Global Helpers.

在C#中,我可以只添加新的帮手无论作为公共静态类,然后将命名空间添加到〜/查看/ Web.config中和关闭我去。尽我所知,C#和VB.NET有很大的不同在这里。

In C# I can merely add new helpers wherever as a public static class, then add the namespace to ~/Views/Web.config and off I go. To the best of my knowledge C# and VB.NET differ here greatly.

在与编译器多少作战和堆栈溢出搜索我发现,如果将它们放置在APP_ code文件夹中我能得到定制的助手在视图中工作。

After much battling with the compiler and Stack Overflow searching I found that I could get the custom helpers to work in views if placed them in the App_Code folder.

我用了℃的模块; System.Runtime.CompilerServices.Extension> 属性应用到每个方法。然后加入〜/查看/ Web.config中和APP_ $ C $文件夹C和汇编。

I used a Module with the <System.Runtime.CompilerServices.Extension> attribute applied to each method. Then added to ~/Views/Web.config and the App_code folder and compile.

Imports System.Web.Mvc
Public Module HtmlHelpers
    <System.Runtime.CompilerServices.Extension>
    Public Function HelperName(ByVal helper As HtmlHelper, hn As HelperName) As MvcHtmlString        
        Return MvcHtmlString.Create(hn.ToString())
    End Function
End Module

这允许我使用新的自定义帮手,但完全阻断了所有的内置HTML辅助就像DisplayFor,EditorFor和ActionLink的。

This allows me to use the new custom helpers, but completely blocks out all of the in-built html helpers like DisplayFor, EditorFor and ActionLink.

请纠正我,如果我错了。似乎任何没有文档中关于这个网上,除了<一个href=\"http://stackoverflow.com/questions/4451287/razor-declarative-html-helpers/4453637#4453637\">confirming问题,则忽略它超过5年。

Please correct me if I'm wrong. There seems to be no documentation whatsoever about this online, apart from confirming the issue, then ignoring it for over 5 years.

我怎么能同时使用?我在我山穷水尽的地步,并失去信心微软的支持/文档​​

How can I use both? I'm at the end of my tether and losing faith in Microsoft's support/documentation.

推荐答案

解决方案:


  1. 不要使用APP_ code。把助手别处。我已经把我的在〜/助手/

  2. 不要使用命名空间。这可能是我的垮台,虽然不能肯定没有进一步测试。

  3. 根项目命名空间添加到〜/查看/ Web.config中

  1. Do not use App_Code. Put the helpers elsewhere. I've put mine in ~/Helpers/
  2. Do not use namespaces. This was probably my downfall, though cannot be sure without further testing.
  3. Add root project namespace to ~/Views/Web.config

<add namespace="MyProjectName" />

不会

<add namespace="MyProjectName.Helpers" />
<add namespace="HtmlHelpers" />
<add namespace="OrAnythingLikeThis" />


  • 确保佣工在构建时被编译(Class.vb>属性>生成操作:编译)

  • Ensure helpers are being compiled at build time (Class.vb > Properties > Build Action: Compile)
  • 我99%肯定,就是这样。我有问题与BootstrapEditorTemplates,因为他们是C#和我在VB.NET工作。为了解决这个我转换的助手到VB.NET并将它们放置在一个单一的文件HtmlHelpers.vb,在〜/助手/,与我自定义的其余部分。这样做可能不是所有必要的;你很可能只需删除在C#帮助文件的名称空间完成相同的,但我没有测试。

    I'm 99% sure that's it. I had issues with BootstrapEditorTemplates as they were C# and I'm working in VB.NET. To solve this I converted the helpers to VB.NET and placed them all in a single HtmlHelpers.vb file, in ~/Helpers/, with the rest of my custom ones. Doing this probably wasn't all necessary; you could probably accomplish the same by just removing the namespace in the C# helper files, but I haven't tested.

    如果这不工作,然后给我留言(在这个问题上,而不是PM)和我会回来,并尝试解决这个问题。

    If this doesn't work then give me a shout (on this question, not pm) and I'll come back and try to solve it.

    这篇关于VB.NET和MVC 5使用自定义佣工内置佣工和无APP_ code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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