MVC3路由问题(隐藏属性名称)

发布于 2024-12-11 04:07:30 字数 1227 浏览 0 评论 0原文

嘿! 我有这个控制器:

      public ViewResult Hotel(string hotelSupplierCode, bool displayAllRooms, bool resend)
    {
        HotelModel model;
        if (resend)
        {
            model = (HotelModel)Session["HotelDetails"];
            HotelManager.ResendHotel(model.Hotel.Id);
            model.Hotel.Status = 1;
        }
        else
        {
            model = HotelModel.GetGotel(hotelSupplierCode, displayAllRooms);
        }
        Session["HotelDetails"] = model;
        return View("Hotel", model);
    }

和这条路线:

 routes.MapRoute(
            "Hotel", // Route name
            "{controller}/{action}/{hotelSupplierCode}/{displayAllRooms}/{resend}", // URL with parameters
            new { controller = "Hotel", action = "Hotel", hotelSupplierCode = UrlParameter.Optional, displayAllRooms = UrlParameter.Optional, resend = UrlParameter.Optional }

问题是,当我访问视图时,返回的 URL 是这样的:

http://localhost:49575/Hotel/Hotel?hotelSupplierCode=3711&displayAllRooms=False&resend=False

但我想要这样的东西:

http://localhost:49575/Hotel/Hotel/3711/False/False

那么我如何隐藏属性名称?如果我手动输入第二个 URL,它就可以正常工作。

Hy!
I have this controller:

      public ViewResult Hotel(string hotelSupplierCode, bool displayAllRooms, bool resend)
    {
        HotelModel model;
        if (resend)
        {
            model = (HotelModel)Session["HotelDetails"];
            HotelManager.ResendHotel(model.Hotel.Id);
            model.Hotel.Status = 1;
        }
        else
        {
            model = HotelModel.GetGotel(hotelSupplierCode, displayAllRooms);
        }
        Session["HotelDetails"] = model;
        return View("Hotel", model);
    }

and this route:

 routes.MapRoute(
            "Hotel", // Route name
            "{controller}/{action}/{hotelSupplierCode}/{displayAllRooms}/{resend}", // URL with parameters
            new { controller = "Hotel", action = "Hotel", hotelSupplierCode = UrlParameter.Optional, displayAllRooms = UrlParameter.Optional, resend = UrlParameter.Optional }

The problem is that when I access the view the returned URL is soemthing like that:

http://localhost:49575/Hotel/Hotel?hotelSupplierCode=3711&displayAllRooms=False&resend=False

but I want something like that:

http://localhost:49575/Hotel/Hotel/3711/False/False

So how I can hide the atribute names? If i put the second URL manualy it works fine.

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

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

发布评论

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

评论(2

眼眸印温柔 2024-12-18 04:07:30

我怀疑你的问题是由于你的路线顺序造成的。路由必须按照从最具体到最一般的顺序放置,通常以新项目中定义的默认路由结尾。

确保将酒店路线放置在默认路线之前。

I suspect that your issue is due to the order of your routes. Routes must be placed in order from the most specific to the most general, usually ending with the default route defined in a new project.

Make certain that you place the Hotel route before the default route.

简美 2024-12-18 04:07:30

尝试这样的路线

routes.MapRoute(
            "Hotel",
            "Hotel/Hotel/{hotelSupplierCode}/{displayAllRooms}/{resend}",
            new { controller = "Hotel", action = "Hotel" }, 
            new { hotelSupplierCode = @"\w+",displayAllRooms = @"\w+", resend= @"\w+"}
        );

Try route like this

routes.MapRoute(
            "Hotel",
            "Hotel/Hotel/{hotelSupplierCode}/{displayAllRooms}/{resend}",
            new { controller = "Hotel", action = "Hotel" }, 
            new { hotelSupplierCode = @"\w+",displayAllRooms = @"\w+", resend= @"\w+"}
        );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文