简体   繁体   中英

Use a class in aspx code behind

First of all I would like to say that I have not experience with the .NET framework. I've the following problem. From Javascript, I perform a call to an aspx page, which has the following c# code behind:

using System;
using Decoder;

namespace Decoder
{
public class PDF_Generator : System.Web.UI.Page
{
private string decodedPDF;
private string base64EncodedPDF;
Decoder.Decoder decoder = new Decoder();

protected void Page_Load(object sender, EventsArgs e)
{
    try
    {
        this.base64EncodedPDF = Request.Params["encodedString"]; //get encoded string from js
        this.decodedPDF = decoder.decodeFromBase64toString(this.base64EncodedPDF); //decode string

        byte[] pdfByteStream = decoder.getBytesFromString(this.decodedPDF);

        Response.Clear();
        Response.ContentType = "application/pdf";
        //Response.AddHeader("Content-Disposition", "attachment; filename=\"summary.pdf\"");
        Response.AddHeader("Content-Length", pdfByteStream.Length.toString());
        Response.BinaryWrite(pdfByteStream);
        Response.End();
    }
}
}
}

I would like to use my class Decoder.cs in PDF_Generator, but I receive the following error:

CS0234: The type or namespace 'Decoder' does not exist in the class or namespace 'Decoder' (are you missing an assembly reference?)

My aspx file is this:

<%@ Import Namespace="Decoder"%>
<%@ Page language="c#" src="Scipafi_PDF.aspx.cs" AutoEventWireup="true" Inherits="PDF_Generator" %>

Thanks in advance for all your advices.

You are importing the Decoder Namespace twice, once in your code behind ( using Decoder; ) and once in your aspx page ( <%@ Import Namespace="Decoder"%> ). I wonder if that may be the problem. Try removing

<%@ Import Namespace="Decoder"%>

from your aspx page.

If looks like you've re-written code for things that are already in the .Net framework. See System.Convert.ToBase64String() and System.Convert.FromBase64String()

you should remove from the page

<%@ Import Namespace="Decoder"%>

and make Inherits="Decoder.PDF_Generator"

<%@ Page language="c#" src="Scipafi_PDF.aspx.cs" AutoEventWireup="true" Inherits="Decoder.PDF_Generator" %>

you also don't need in codebehind and can remove

using Decoder;

but this actually shouldn't raise errors

UPDATE

take a look at @Ryios comment, he is right

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