0

I created an aspx file and want to using a class from other project.
I tried using <%@ Import Namespace="DTO" %> in the aspx file, it showed 0 error, but when I run the project, the aspx file showed error: CS0246: The type or namespace name 'DTO' could not be found.
I also tried using the code below in Web.config file:

<pages>
          <namespaces>
            <add namespace="DTO"/>
          </namespaces>
</pages>

It also showed 0 error, but when I run the project the aspx file show the same error.
I had added reference to other project, both my project using .Net Framework 4.5.2.
The visual studio always showed 0 error, but the aspx file always showed "CS0246: The type or namespace name 'DTO' could not be found." when I run.

Edit This is the code I use in aspx file:

<%@ Page Title="" Language="C#" MasterPageFile="~/Default.Master" AutoEventWireup="true" CodeBehind="cart.aspx.cs" Inherits="Demo_MasterPage.cart" %>
<%@ Import Namespace="DTO" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <%
        List<OrderItem> cart = Session["cart"] as List<OrderItem>;
    %>
</asp:Content>

and this is the code of OrderItem class

namespace DTO
{
    public class OrderItem
    {
        public int OrderId { get; set; }
        public string ProductId { get; set; }
        public int Quantity { get; set; }
        public OrderItem()
        {

        }
        public OrderItem(int orderId, string productId, int quantity)
        {
            OrderId = orderId;
            ProductId = productId;
            Quantity = quantity;
        }
    }
}
2
  • Did you try to use import with namespace? Could you show us DTO class and the NameSpace you use in Import? Commented Jul 13, 2020 at 19:05
  • I had tried '<%@ Import Namespace="DTO" %>' in my aspx file but it didn't work. It didn't show any error in the visual studio but always show error when I run Commented Jul 14, 2020 at 4:26

1 Answer 1

1

You need to use the fully qualified namespace of the assembly you would like to import. It means that you should add project name that you have referenced too:

<%@ Import Namepace="myProject.DTO"%>

See How to import a custom namespace from a dll file into .aspx page

In addition when you do set the reference, make sure the copy local property is set to true.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.