启用仅显示查询字符串值的路由 [英] Enable a route that displays query string values only

查看:78
本文介绍了启用仅显示查询字符串值的路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够有一个类似的路线

I want to be able to have a route look like

[Route("{foo}/{bar}")]



ActionResult索引(字符串foo,字符串栏) {}



每个Url然后使用那个ActionResult,

主页/索引主页/联系人OtherFoo / OtherBar都会指向索引因为声明的路线。



我的尝试:



现在我有和所有其他路线按预期工作。


ActionResult Index(string foo, string bar){}

Every Url then uses that ActionResult,
Home/Index Home/Contact OtherFoo/OtherBar would all point to the Index because of the declared route.

What I have tried:

Right now I have and all other routes work as expected.

[Route("static/{foo}/{bar}")]

推荐答案

我制作路线,但效果相同,家庭/联系人路由到TestIndex。



I make the route, but it has the same effect, home/contact is routed to the TestIndex.

routes.MapRoute(
               name: "TestRoute",
               url: "{foo}/{bar}",
               defaults: new { controller = "FooBar", action = "TestIndex", foo = UrlParameter.Optional, bar = UrlParameter.Optional }
           );


public class FooBarController : Controller
   {
       // GET: TestIndex
       public ActionResult TestIndex(string foo, string bar)
       {


           return View();
       }
   }





感谢您的建议!



Thanks for the suggestion!


我不认为你要求在基于属性的路由中工作。你必须通过常规路由做你想做的事。



I don't think what you are asking for works in attribute based routing. Your going to have to do what you want via conventional routing.

routes.MapRoute(
                "HomeIndexParamOnlyRoute",
                "{foo}/{bar}",
                new {controller = "Home", action = "Index"}
                );





将它放在RouteConfig.cs中。确保它在默认路线之前(下图)。





Place that in your RouteConfig.cs. Make sure it is before the default route (below).

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );





然后上面的HomeIndexParamOnlyRoute将允许您使用类似http:// localhost:51830 / foo / bar的URL,而不必执行http:// localhost:51830 / home / index / foo / bar



Then the HomeIndexParamOnlyRoute above will allow you to use a URL like http://localhost:51830/foo/bar instead of having to do http://localhost:51830/home/index/foo/bar


这篇关于启用仅显示查询字符串值的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆