URL编码时报错404

Error 404 when URL is encoded

首先我不得不说我是一个初学者。我正在使用 asp.net C# 为我的大学产品开发一个音乐商店。

我为路径 http://localhost:1375/~/Pages/Product.aspx?id=2 编码了 URL 通过

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

我收到错误 404:

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

我知道我必须在某处解码编码的 URL,但我不知道在哪里。 我给出以下代码:

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();
    }
}

}

您不应该对整个 url 进行编码。那会导致错误。仅在第一个之后编码?象征。即

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

只对上面url问号后的id部分进行编码

详情请参考this question

检查this answer编码参数部分