MVC3 映射路由,其中​​ URL 的第一个值是默认控制器和操作的查询字符串

发布于 2024-11-23 16:24:10 字数 769 浏览 1 评论 0原文

我想制作一条类似这样的路线:

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

其中 s 是默认控制器和操作的参数。这可能吗?我也会满足于类似的东西:

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

        routes.MapRoute(
            "qMap", // Route name
            "sc/{s}", // URL with parameters
            new { controller = "Home", action = "Index", s = UrlParameter.Optional } 
        );

但两者都不起作用..我怀疑是因为第一个元素/参数总是期望是控制器?

I want to make a route something like this:

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

Where s is a parameter for the default controller and action.. Is this possible? i would also settle for something like:

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

        routes.MapRoute(
            "qMap", // Route name
            "sc/{s}", // URL with parameters
            new { controller = "Home", action = "Index", s = UrlParameter.Optional } 
        );

But neither work.. i suspect because the first element/paramater is always expected to be a controller?

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

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

发布评论

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

评论(1

同展鸳鸯锦 2024-11-30 16:24:10

如果您只有以下路由定义(确保已从 RegisterRoutes 方法中删除所有其他路由定义):

routes.MapRoute(
    "Default",
    "{s}",
    new { controller = "Home", action = "Index", s = UrlParameter.Optional } 
);

和以下控制器:

public class HomeController: Controller
{
    public ActionResult Index(string s) 
    {
        ...
    }
}

表单 http://foo.com/abc 的请求将被路由到 Home 控制器,并且 Index 操作将被调用并传递 s=abc

If you have only the following route definition (make sure you have removed all other route definitions from your RegisterRoutes method):

routes.MapRoute(
    "Default",
    "{s}",
    new { controller = "Home", action = "Index", s = UrlParameter.Optional } 
);

and the following controller:

public class HomeController: Controller
{
    public ActionResult Index(string s) 
    {
        ...
    }
}

a request of the form http://foo.com/abc would be routed to the Home controller and the Index action will be invoked and passed s=abc.

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