MVC 路由不起作用

发布于 2024-09-13 18:47:37 字数 1751 浏览 7 评论 0原文

更新:

正如有人指出的那样,我在路线注册中缺少 s。现在我有一个次要问题。

这就是我希望它工作的方式:

http://localhost/products/ --> ProductsController.Index()

http://localhost/products/3/apples --> ProductsController.Details(int? id, string ProductName)

这是当前发生的情况:

http://localhost/products 转到我的详细信息操作。

我该如何为此设置路线?

我在 Global.asx 文件中设置了以下路由:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "ViewProduct",
        "products/{id}/{productName}",
        new { controller = "Products", action = "Details", id = 0, productName = "" } // Parameter defaults
    );

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

我有以下控制器:

public class ProductsController : Controller
{
    //
    // GET: /Products/

    public ActionResult Index()
    {
        IList<Product> products = new List<Product>
        {
            new Product { Id = 57349, ProductName = "Apple" },
            new Product { Id = 57350, ProductName = "Banana" }
        };

        return View(products);
    }

    public ActionResult Details(int? id, string productName)
    {
        Product p = new Product { Id = id.Value, ProductName = productName };

        return View(p);
    }
}

Update:

As someone pointed out I was missing an s in my Route registration. Now I have a secondary problem.

This is how I want it to work:

http://localhost/products/ --> ProductsController.Index()

http://localhost/products/3/apples --> ProductsController.Details(int? id, string productName)

This is what currently happens:

http://localhost/products goes to my Details action.

How do I set up the routes for this?

I set up the following routes in my Global.asx file:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "ViewProduct",
        "products/{id}/{productName}",
        new { controller = "Products", action = "Details", id = 0, productName = "" } // Parameter defaults
    );

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

I have the following controller:

public class ProductsController : Controller
{
    //
    // GET: /Products/

    public ActionResult Index()
    {
        IList<Product> products = new List<Product>
        {
            new Product { Id = 57349, ProductName = "Apple" },
            new Product { Id = 57350, ProductName = "Banana" }
        };

        return View(products);
    }

    public ActionResult Details(int? id, string productName)
    {
        Product p = new Product { Id = id.Value, ProductName = productName };

        return View(p);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

活雷疯 2024-09-20 18:47:37

您的路由声明操作中缺少一个“s”

=“Details

,或者您的操作名称中多了一个“s”:

public ActionResult Details(int ? id, string ProductName)

由您决定要纠正哪一个;)

更新:对于路线更新,只需使用:

routes.MapRoute(
    "ViewProduct",
    "products/{id}/{productName}",
    new { 
        controller = "Products", 
        action = "Details" }
);

因此,当您输入 /products 时,将使用“默认”路线。

You're missing a "s" in your route declaration

action = "Details"

Or have one exceeding "s" in your action name :

public ActionResult Details(int? id, string productName)

It's up to you to decide which one to correct ;)

Update : for the route update just use :

routes.MapRoute(
    "ViewProduct",
    "products/{id}/{productName}",
    new { 
        controller = "Products", 
        action = "Details" }
);

So when you type /products the "Default" route gets used.

前事休说 2024-09-20 18:47:37

因为您已经定义了 idproductName 的默认值,所以路由引擎只会在缺少这些值时为您填充这些值,因此您对 /products 的请求将插入这些默认值,并且进入细节行动。

取出:id = 0, ProductName = "",您将获得预期的行为。

编辑

考虑在其中添加一个 {action} 参数。由于您有默认操作并且无法覆盖它,因此您可能会将所有内容都路由到详细信息。

Because you have defined default values for id and productName the routing engine just fills those in for you when they are missing, so your request to /products is getting those defaults inserted and going to the details action.

Take out: id = 0, productName = "" and you'll get the expected behavior.

edit

Consider having an {action} parameter in there. Since you have a default action and no way to override it you may have everything routed to details anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文