ASP.NET MVC 路由问题

发布于 2024-10-26 12:23:44 字数 1054 浏览 3 评论 0原文

我有关于 MVC 路由的简单问题。我如何构建生成以下链接的 Html.ActionLink http://mysite.com/phones/samsung< br> 现在它生成为 http://mysite.com/phones/brand?brand=samsung
另外我想避免在 URL 中提及操作名称
有我的代码:

路线:

routes.MapRoute(null, "Phones/{brand}", 
new { controller = "Phones", action = "Index", brand = UrlParameter.Optional });

控制器:

MySyteDBEntities ms = new MySyteDBEntities();

public ActionResult Index()
{
    ViewBag.Brand = ms.Phones.Select(x => x.Brand).Distinct();
    return View();            
}

public ActionResult Brand(string brand)
{
    ViewBag.Standard = ms.Phones.Where(x => x.Brand == brand).Select(x => x.Standard).Distinct();
        return View();
}

索引视图代码:

@foreach (string item in ViewBag.Brand)
{                    
    <div>@Html.ActionLink(item, "Brand", new { brand = item })</div>
}

I have simple question about MVC routing. How i can construct Html.ActionLink thhat generates following link http://mysite.com/phones/samsung
Now it's generates as http://mysite.com/phones/brand?brand=samsung
Also i want to avoid mentioning action name in URL
There is my code:

Route:

routes.MapRoute(null, "Phones/{brand}", 
new { controller = "Phones", action = "Index", brand = UrlParameter.Optional });

Controller:

MySyteDBEntities ms = new MySyteDBEntities();

public ActionResult Index()
{
    ViewBag.Brand = ms.Phones.Select(x => x.Brand).Distinct();
    return View();            
}

public ActionResult Brand(string brand)
{
    ViewBag.Standard = ms.Phones.Where(x => x.Brand == brand).Select(x => x.Standard).Distinct();
        return View();
}

Index View code:

@foreach (string item in ViewBag.Brand)
{                    
    <div>@Html.ActionLink(item, "Brand", new { brand = item })</div>
}

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

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

发布评论

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

评论(5

一笔一画续写前缘 2024-11-02 12:23:44

在您的 MapRoute 中,您没有空间用于操作,因此 ASP.NET 将始终使用默认操作“索引”。

默认情况下,您的路由将如下所示:

routes.MapRoute(" Default", "{controller}"/{action}/{id}", 
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }

您缺少操作部分。

操作链接中的路由值与路由中的参数不匹配,将是查询字符串参数。因此,您需要将路线中的“类别”更改为“品牌”。

试试这个:

routes.MapRoute(null, "Phones/{brand}", 
 new { controller = "Phones", action = "Index", brand = UrlParameter.Optional });

@foreach (string item in ViewBag.Brand)
{                    
 <div>@Html.ActionLink(item, "Index", "Phones", new { brand = item }, null)</div>
}

如果当前视图是通过另一个路由映射的,请务必在 ActionLink 中显式调用控制器,否则它无法识别 brand 参数

In your MapRoute you have no space for an action, so asp.net will always use the default action "Index".

By default your routing would look like this:

routes.MapRoute(" Default", "{controller}"/{action}/{id}", 
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }

You're missing the action part.

Routevalues in you actionlink which don't match parameters in your route, will be querystring parameters. So you need to change "category" to " brand" in your route.

Try this:

routes.MapRoute(null, "Phones/{brand}", 
 new { controller = "Phones", action = "Index", brand = UrlParameter.Optional });

and

@foreach (string item in ViewBag.Brand)
{                    
 <div>@Html.ActionLink(item, "Index", "Phones", new { brand = item }, null)</div>
}

Be sure to call the controller explicit in your ActionLink, if the current view is mapped through another route, otherwise it doesn't recognize the brand parameter.

小鸟爱天空丶 2024-11-02 12:23:44

尝试(如果有的话,应该在默认路由之前注册此路由)

        routes.MapRoute(
         "Phones", // Route name
         "Phones/{[^(Index)]brand}", // URL with parameters
         new { controller = "Phones", action = "Brand", brand = "" } // Parameter defaults
     );

有了这个, http://mysite.com/电话/ -->应该转到索引操作并且
http://mysite.com/phones/samsung -->;应该去品牌行动。

Try (this route should be registered before the default route, if you have one)

        routes.MapRoute(
         "Phones", // Route name
         "Phones/{[^(Index)]brand}", // URL with parameters
         new { controller = "Phones", action = "Brand", brand = "" } // Parameter defaults
     );

With this, http://mysite.com/phones/ --> should go to Index Action and
http://mysite.com/phones/samsung --> should go to the Brand Action.

一抹苦笑 2024-11-02 12:23:44

我找到了问题的根源。我只需要删除 MapRoute 中的最后一块(品牌可选参数)。野兔是代码:

routes.MapRoute(null, "Phones/{brand}", new { controller = "Phones", action = "Brand" });

i'm found source of problem. I just need to remove last piece (brand optional parameter) in MapRoute. Hare is code:

routes.MapRoute(null, "Phones/{brand}", new { controller = "Phones", action = "Brand" });
迟月 2024-11-02 12:23:44
routes.MapRoute(null, "Phones/{id}", 
new { controller = "Phones", action = "Index", id= UrlParameter.Optional })

public ActionResult Brand(string id)
{
    ViewBag.Standard = ms.Phones.Where(x => x.Brand == brand).Select(x => x.Standard).Distinct();
        return View();
}

通过使用 id 作为参数名称,将阻止路由使用 querystring 键值对。

您的无参数 GET 和 View 代码应该仍然可以工作,无需任何更改。

routes.MapRoute(null, "Phones/{id}", 
new { controller = "Phones", action = "Index", id= UrlParameter.Optional })

public ActionResult Brand(string id)
{
    ViewBag.Standard = ms.Phones.Where(x => x.Brand == brand).Select(x => x.Standard).Distinct();
        return View();
}

By using id as the parameter name, it will prevent the routing from using the querystring key value pairs.

Your parameterless GET and View code should still work without any changes.

记忆消瘦 2024-11-02 12:23:44

如果我没记错的话, {brand} 部分需要作为参数的一部分包含在内:

routes.MapRoute(null, "Phones/{brand}", 
new { controller = "Phones", action = "Index", brand = UrlParameter.Optional });

只需记住,它需要位于任何默认路由之前。

If I remember correctly, the {brand} part needs to be included as is as part of your parameters:

routes.MapRoute(null, "Phones/{brand}", 
new { controller = "Phones", action = "Index", brand = UrlParameter.Optional });

Just remember, it needs to go before any default routes.

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