简体   繁体   English

从视图调用库函数(MVC C#)

[英]Calling a Library Function from a View (MVC C#)

I have a function in my Misc library in App_Code called EncodePicture which encodes a picture. 我在App_Code的Misc库中有一个函数叫做EncodePicture,它编码一张图片。 However when I try to call the code, I get the function exists in both. 但是,当我尝试调用代码时,我在两者中都存在函数。 I've looked at other answer such as clearing down the temporary files, I did that but it didn't work. 我已经查看了其他答案,例如清除临时文件,我这样做但是它没有用。 The Misc Library has No namespace, does it need it? Misc Library没有命名空间,是否需要它?

I'm calling the function as :- 我把这个函数称为: -

<img id="imgTitle" src="data:image/png;base64,@Misc.EncodePicture("/aPic/banner.jpg")" alt="" width="468" height="60" />

I get it Misc exists in both Universe and App_Code..... 我明白Misc存在于Universe和App_Code .....

How can I get round this problem? 我怎样才能解决这个问题? The function is :- 功能是: -

using System.Security.Cryptography;


namespace Universe
{

public class Misc
{

    public static string EncodePicture(string sFilename)
    {
        string sEncode = "";

        using (FileStream fs = new FileStream(HttpContext.Current.Server.MapPath("~" + sFilename), FileMode.Open))
        {

            System.IO.BinaryReader br = new BinaryReader(fs);
            Byte[] bytes = br.ReadBytes((Int32)fs.Length);
            sEncode = Convert.ToBase64String(bytes, 0, bytes.Length);
        }

        return sEncode;
    }

Please don't say hard code the value because I'm going to be using this style in other places and can't hard code all the images. 请不要说硬编码值,因为我将在其他地方使用这种风格,并且不能硬编码所有图像。 C# or VB.NET pls. C#或VB.NET请参阅。

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. 请查看以下特定错误详细信息并相应地修改源代码。

Compiler Error Message: CS0433: The type 'Misc' exists in both 'App_Code.keoe0a1i, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' and 'Universe, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' 编译器错误消息:CS0433:“Misc”类型同时存在于'App_Code.keoe0a1i,Version = 0.0.0.0,Culture = neutral,PublicKeyToken = null'和'Universe,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null “

Qualify via Namespace 通过命名空间限定

Depending on where your EncodePicture() method is defined, you can import the appropriate namespace so that it could be called within your View : 根据您定义EncodePicture()方法的位置,您可以导入相应的命名空间,以便可以在View中调用它:

namespace YourProject
{
    public static class Misc
    {
        public static string EncodePicture(string file)
        {
            // Build a URL for the requested path
            return file;
        }
    }
}

and then simply add a using statement within your View : 然后只需在视图中添加using语句:

@using YourProject;

And you should then be able to call it as expected via : 然后你应该能够按预期通过以下方式调用它:

@Misc.EncodePicture(...)

Or without the using statement in a fully qualified manner : 或者没有完全合格的using语句:

@YourProject.Misc.EncodePicture(...)

Reference it anywhere via the web.config 通过web.config在任何地方引用它

If this was a method you would expect to use throughout various different Views, then you might consider adding the namespaces in your web.config so that it would be accessibly more easily : 如果这是您希望在各种不同视图中使用的方法,那么您可以考虑在web.config添加命名空间,以便更容易访问:

<system.web.webPages.razor>
  <pages pageBaseType="System.Web.Mvc.WebViewPage">
    <namespaces>
      <add namespace="System.Web.Mvc" />
      <add namespace="System.Web.Mvc.Ajax" />
      <!-- Add your namespace here -->
      <add namespace="YourProject" />
    </namespaces>
  </pages>
</system.web.webPages.razor>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM