Thứ Năm, 12 tháng 1, 2023

[.NET] Hướng dẫn cấu hình URL đường dẫn thân thiện

 Cấu hình URL (Đường dẫn thân thiện)

2 loại đường dẫn có thể cấu hình.

- Không có tham số.

- Có tham số.

1. Không có tham số

Đối với đường dẫn không có tham số, ta cấu hình như sau:

RouteConfig.cs

Ví dụ:

routes.MapRoute(

name: "test",   /*Tên của maproute khi gọi đến*/

url: "test",    /*định nghĩa lại url*/

default: new {controller = "testcontroller",action="testAction", id=UrlParrameter.Optional}

);

Tại các view ta muốn tạo link liên kết đến action ta sử dụng cú pháp

@Html.RouteLink("link test" , "test")

Giả sử bây giờ chúng ta muốn thay đổi đường dẫn https://localhost:44332/KhachHang/Index thành https://localhost:44332/khach-hang

Đầu tiên vào Folder App_Start , Mở file RouteConfig.cs, thêm MapRoute

routes.MapRoute(

                name:"khachhang",

                url: "khach-hang",

                defaults: new {controller="KhachHang",action="Index"}

                );

Nhúng link muốn trỏ để URL sau khi rewrite bằng mã bên dưới

@Html.RouteLink("Click vào đây để vào trang khách hàng","khachhang")

2. ĐỐI VỚI ĐƯỠNG DẪN CÓ THAM SỐ 

Cấu hình trong File RoutingConffig.cs

Ví dụ

routes.MapRoute(

            name: "XemChiTiet",

            url: "{tensp}-{id}",

            defaults: new { controller = "SanPham", action = "ChiTietSanPham", id = UrlParrameter.Optional }

            );

SanPhamController.cs

public ActionResult ChiTietSanPham(int? id , string tensp)

        {

            if (id == null)

            {

                return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest);

            }

            SanPham sp = db.SanPhams.SingleOrDefault(n => n.MaSP == id);

            if (sp==null)

            {

                return HttpNotFound();

            }

            return View(sp);

        }

 CODE THAM KHẢO

Trong RouteConfig

routes.MapRoute(

                name: "XemChiTiet",

                url: "{tensp}-{id}",

                defaults: new { controller = "SanPham", action = "ChiTietSanPham", id = UrlParameter.Optional }

            );


Trong controller SanPhamController

public ActionResult ChiTietSanPham(int? id , string tensp)

        {

            if (id == null)

            {

                return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest);

            }

            SanPham sp = db.SanPhams.SingleOrDefault(n => n.MaSP == id);

            if (sp==null)

            {

                return HttpNotFound();

            }

            return View(sp);

        }

Trong View SanPham

<div class="col-md-9 product1">

                <div class=" bottom-product">

                    @foreach (var item in Model.OrderBy(n=>n.DonGia))

                    {

                    <div class="col-md-4 bottom-cd simpleCart_shelfItem">

                        <div class="product-at ">

                            @*<a href="@Url.Action("ChiTietSanPham","SanPham",new { id=item.MaSP})">*@

                            <a href="@Url.RouteUrl("XemChiTiet",new { @id=item.MaSP,tensp=@item.TenSP})">

                                <img class="img-responsive" src="~/Content/images/@item.HinhAnh" alt="">

                                <div class="pro-grid">

                                    <span class="buy-in">Buy Now</span>

                                </div>

                            </a>

                        </div>

                        <p class="tun">@item.MoTa</p>

                        <a href="#" class="item_add"><p class="number item_price"><i> </i>@item.DonGia.Value.ToString("#,##")</p></a>

                    </div>

                    }

                </div> 

            </div>



Không có nhận xét nào:

Đăng nhận xét