1

First of all I have to say I'm a beginner. I'm developing a music shop for my college product in asp.net C#.

I encoded the URL for the path http://localhost:1375/~/Pages/Product.aspx?id=2 by

a = Server.UrlEncode(string.Format("~/Pages/Product.aspx?id={0}",product.Id));

And I get error 404:

The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

I know I have to decode the encoded URL somewhere, but I'm confused where. I'm giving the code below:

Index.aspx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    string a;
    ProductModel model = new ProductModel();
    List<Product> products = model.GetAllProducts();

    if (products != null)
    {
        foreach (Product product in products)
        {
            Panel productPanel = new Panel();

            a = Server.UrlEncode(string.Format("~/Pages/Product.aspx?id=    {0}",product.Id));

            ImageButton imageButton = new ImageButton
            {
                ImageUrl = "~/ABCDIMGS/img/" + product.Image,
                CssClass = "productImage",

                PostBackUrl = a
            };
            Label lblName = new Label
            {
                Text = product.Name,
                CssClass = "productName"
            };
            Label lblPrice = new Label
            {
                Text = "$ " + product.Price,
                CssClass = "productPrice"
            };

            productPanel.Controls.Add(imageButton);
            productPanel.Controls.Add(new Literal { Text = "<br/>" });
            productPanel.Controls.Add(lblName);
            productPanel.Controls.Add(new Literal { Text = "<br/>" });
            productPanel.Controls.Add(lblPrice);

            //Add dynamic controls to static control
            pnlProducts.Controls.Add(productPanel);
        }
    }
    else
        pnlProducts.Controls.Add(new Literal { Text = "No products found!" });
}
}

Product.aspx

using System;
using System.Linq;
using Microsoft.AspNet.Identity;

public partial class Pages_Product : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    FillPage();
}

protected void btnAdd_Click(object sender, EventArgs e)
{
    if (!string.IsNullOrWhiteSpace(Request.QueryString["id"]))
    {
        string clientId = Context.User.Identity.GetUserId();
        if (clientId != null)
        {

            int id = Convert.ToInt32(Request.QueryString["id"]);
            int amount = Convert.ToInt32(ddlAmount.SelectedValue);

            Cart cart = new Cart
            {
                Amount = amount,
                ClientID = clientId,
                DatePurchased = DateTime.Now,
                IsInCart = true,
                ProductID = id
            };

            CartModel model = new CartModel();
            lblResult.Text = model.InsertCart(cart);
        }
        else
        {
            lblResult.Text = "Please log in to order items";
        }
    }
}

private void FillPage()
{
    //Get selected product data
    if (!string.IsNullOrWhiteSpace(Request.QueryString["id"]))
    {
        int id = Convert.ToInt32(Request.QueryString["id"]);
        ProductModel model = new ProductModel();
        Product product = model.GetProduct(id);

        //Fill page with data
        lblTitle.Text = product.Name;
        lblDescription.Text = product.Description;
        lblPrice.Text = "Price per unit:<br/>$ " + product.Price;
        imgProduct.ImageUrl = "~/ABCDIMGS/img/" + product.Image;
        lblItemNr.Text = product.Id.ToString();

        //Fill amount list with numbers 1-20
        int[] amount = Enumerable.Range(1, 20).ToArray();
        ddlAmount.DataSource = amount;
        ddlAmount.AppendDataBoundItems = true;
        ddlAmount.DataBind();
    }
}

}
Ghasem
  • 14,455
  • 21
  • 138
  • 171

1 Answers1

0

You shouldn't encode the entire url. That will cause error. Encode only after the first ? symbol. That is

~/Pages/Product.aspx?id={0}

Only encode the id part in the above url after the question mark.

Please refer this question for more details.

Check this answer for encoding parameter part

Community
  • 1
  • 1