简体   繁体   中英

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. 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?

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.....

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.

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'

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 :

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 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 :

@YourProject.Misc.EncodePicture(...)

Reference it anywhere via the 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 :

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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