簡體   English   中英

ASP.NET MVC路由“ / category / {string}”而不是“ / category / details / {string}”

[英]ASP.NET MVC routing '/category/{string}' instead of '/category/details/{string}'

我想創建一條訪問我的類別的路由,例如: /category/{id}而不是/category/details/{id}

我已經嘗試過很多可能的例子

routes.MapRoute(
    "Category",
    "category/{id}",
    new { controller = "category", action = "details", id = UrlParameter.Optional },
    new string[] { "Project.Controllers" }
);

但它似乎不起作用。 有人可以幫我弄這個嗎?

這是我的指揮官

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Project.Controllers
{
    public class CategoryController : Controller
    {
        public ActionResult Details(string id)
        {
            return Content(id);
        }
    }
}

我正確地完成了所有操作,只是將其放置在默認路線下方,需要將新的潰敗點置於默認路線上方

乍一看它是正確的,但您也可以有其他路線,這取決於位置。 使用Glimpse調試應用程序: 本周NuGet軟件包#5-使用Glimpse調試ASP.NET MVC應用程序

我只是在本地機器localhost / Category / Details / 1上嘗試過

routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Category", action = "Details", id = UrlParameter.Optional     } // Parameter defaults
        );



public class CategoryController : Controller
{
    //
    // GET: /Category/

    public ActionResult Details(string id)
    {
        return View();
    }

}

還是在做HttpGet或HttpPost?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM