编码网址时出现错误404

时间:2016-01-10 05:53:15

标签: c# asp.net encoding decoding

首先,我必须说我是初学者。我正在asp.net C#为我的大学产品开发一家音乐商店。

我编码了路径http://localhost:1375/~/Pages/Product.aspx?id=2的网址 由

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

我得到错误404:

  

您要查找的资源已被删除,名称已有   已更改,或暂时无法使用。

我知道我必须在某处解码编码的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();
    }
}

}

1 个答案:

答案 0 :(得分:0)

您不应该编码整个网址。那会导致错误。只在第一次之后编码?符号。那是

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

仅在问号后面的上述网址中编码id部分。

请参阅this question了解详情。

检查this answer是否有编码参数部分