MVC佣工工作内联,但不是在APP_ code [英] MVC Helpers Working inline, but not in App_Code

查看:130
本文介绍了MVC佣工工作内联,但不是在APP_ code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本来我重复每个菜单项和$ C $行C只是很难codeD的各种菜单项值,但后来我碰到助手,教我想给它一个尝试。现在,code(每个菜单项)的6线减少到一个(每个项目),我有一个地方去,而不是改变5处更改它的任何东西。所有伟大的东西。这里是code:

Originally I repeated lines of code for each menu item and just hard coded the various menu item values but then I came across helpers and taught I would give it a try. Now 6 lines of code (for each menu item) are reduced to one (for each item), and I have a single place to go to alter anything instead of changing it in 5 places. All great stuff. Here is the code:

@helper MenuItem(string action, string controller)
{
    <a href="@Url.Action(action, controller)" id="@controller">
        <div class="MenuItem">
            <img src="@("/XXX.YYY.Web/Content/Images/Icons/Menu/mnu"+controller+".png")" /><br />
            //I had to put the XXX.YYY as a literal string because the ~ didn't work, it was quoted literally also instead of showing the home folder.
            @controller
        </div>
    </a>
}

我的问题是,它的作品时,我用它内联,说我_Layout.cshtml的顶部code以下行:

My problem is that it works when I use it inline, say at the top of my _Layout.cshtml with the following lines of code:

        @MenuItem("Index", "Home")
        @MenuItem("Index", "Chart")

但是,当我将其删除出到APP_ code文件夹下称为LayoutHelpers.cshtml一个通用的帮手,所以我可以重复使用它,并改变code相应如下:

But when I remove it out to a generic helper called LayoutHelpers.cshtml under the App_Code folder so I can reuse it, and alter the code accordingly as follows:

        @LayoutHelpers.MenuItem("Index", "Home")
        @LayoutHelpers.MenuItem("Index", "Chart")

请注意:在实际的帮手什么也没有改变。只有2行以上的_Layout.cshtml文件改变了。

Note: Nothing in the actual helper changed. Only the above 2 lines in the _Layout.cshtml file changed.

当我做这些改变,我得到了以下错误:

When I make those changes I get the following error:

编译错误
  说明:该请求提供服务所需资源的编译过程中出现错误。请检查下列特定错误详细信息并适当地修改源$ C ​​$ C。

Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

编译器错误信息:CS0103:'URL'这个名字并不在当前的背景下存在

Compiler Error Message: CS0103: The name 'Url' does not exist in the current context

源错误:

3号线:@helper菜单项(串动,串控制器)
4号线:{
5号线:
6号线:
7号线:@的
的@

Line 3: @helper MenuItem(string action, string controller) Line 4: { Line 5: Line 6: Line 7: @
@

现在好奇的是,发现它是如何工作的第7行mnuHome.png,而不是mnucontroller.png。然而,它说,5号线是错误的。

Now the curious thing is, notice how it works on line 7 "mnuHome.png" as opposed to mnucontroller.png. Yet it says that line 5 is in error.

我也有与问题〜在助手无法正常工作。 IE浏览器。在〜/内容显示为一个文本字符串,而不是它被编译成一条合适的路径应该始终指向应用程序的主文件夹。

I also have a problem with the ~ not working in the helper. ie. the ~/Content is shown as a literal string instead of it being compiled to a proper path which should always point to the home folder of the app.

以下是我使用的参考链接:

Following is a link that I am using for reference:

<一个href=\"http://weblogs.asp.net/jgalloway/archive/2011/03/23/comparing-mvc-3-helpers-using-extension-methods-and-declarative-razor-helper.aspx\" rel=\"nofollow\">http://weblogs.asp.net/jgalloway/archive/2011/03/23/comparing-mvc-3-helpers-using-extension-methods-and-declarative-razor-helper.aspx

具体比的一路下跌标题下的剃刀声明助手的页面1/4以下。从这里开始。

Specifically less than 1/4 of the way down the page under the heading "Razor Declarative Helpers". From here on.

在此先感谢您的帮助。

推荐答案

标准佣工(如UrlHelper和的HtmlHelper)不是在一个剃刀直列可用 @helper 。如果你需要使用它需要通过UrlHelper作为参数传递给你的助手:

The standard helpers (such as UrlHelper and HtmlHelper) are not available in a Razor inline @helper. If you need to use it will need to pass the UrlHelper as parameter to your helper:

@helper MenuItem(UrlHelper url, string action, string controller)
{
    <a href="@url.Action(action, controller)" id="@controller">
        <div class="MenuItem">
            <img src="@url.Content("~/XXX.YYY.Web/Content/Images/Icons/Menu/mnu"+controller+".png")" />
            <br />
            @controller
        </div>
    </a>
}

,然后调用传递正确的实例时:

and then when calling pass the correct instance:

@LayoutHelpers.MenuItem(Url, "Index", "Home")
@LayoutHelpers.MenuItem(Url, "Index", "Chart")

这篇关于MVC佣工工作内联,但不是在APP_ code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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