Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hide parameters passing to controller in address bar (URL rewrite or something else)

I have the following routes:

routes.MapRoute("Event Overview", "{city}/{type}/{id}",
                            new {city="LA", controller = "BaseEvent", action = "EventOverview"}, new {city = new CityConstraint()});

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

And, several links on my site:

@Html.ActionLink("Make", "EventOverview", "BaseEvent", new { id = eventInfo.Key.OID, type = eventInfo.Key.XPObjectType.TypeName.GetShortTypeName(), activeTab = "#scheduleLink", session = eventInfo.Key.EventSchedules[0].SessionId, hall = eventInfo.Key.EventSchedules[0].HallId, client = eventInfo.Key.EventSchedules[0].BasePlace.PremieraClientId}, null)

@Html.ActionLink("Make", "EventOverview", "BaseEvent", new { id = eventInfo.Key.OID, type = eventInfo.Key.XPObjectType.TypeName.GetShortTypeName(), activeTab = "#scheduleLink",  }, null)

This is `EventOverview action:

 public ActionResult EventOverview(int id, string type, string activeTab,string hall, string session, string client, string count)
        {
            var model = CreateEventViewData<EventViewData>(id, type);
            model.ActiveTab = activeTab;
            model.ScheduleCount = count;
            model.SessionId = session;
            model.HallId = hall;
            model.ClientId = client;
            return View("Controls/EventsInfo/EventInfo", model);
        }

In the first link passing many parameters, and all shows in browser's address field:
This is for firts link:

http://localhost:62291/LA/Film/36?activeTab=%23scheduleLink&session=15&hall=65&client=2&count=1

This is for second link:

http://localhost:62291/LA/Film/36?activeTab=%23scheduleLink

I want something like that:

http://localhost:62291/LA/Film/36

What ways to hide parameters in an address line are?

UPDATE:

$(document).ready(function () {
        var link = $(".btn_buy_ticket").find("a").click(function (e) {
            e.preventDefault();            
            $.post($(this).attr("href"));
        });
    })

[HttpPost]
        public ActionResult EventOverview(int id) // just for test
        {
            return RedirectToAction("EventOverview", new {id = id});
        }

        public ActionResult EventOverview(int id, string type, string activeTab,string hall, string session, string client, string count)
        {
            var model = CreateEventViewData<EventViewData>(id, type);
            model.ActiveTab = activeTab;
            model.ScheduleCount = count;
            model.SessionId = session;
            model.HallId = hall;
            model.ClientId = client;
            return View("Controls/EventsInfo/EventInfo", model);
        }

All actions are called, but my EventInfo view not loaded.

like image 550
user348173 Avatar asked Apr 19 '12 04:04

user348173


1 Answers

You could use POST instead of GET. So you could replace the link with a form containing hidden fields for the parameters that you don't want to appear in the query string:

@using (Html.BeginForm("EventOverview", "BaseEvent", new { id = eventInfo.Key.OID, type = eventInfo.Key.XPObjectType.TypeName.GetShortTypeName() }, FormMethod.Post, null))
{
    @Html.Hidden("activeTab", "#scheduleLink")
    @Html.Hidden("session", eventInfo.Key.EventSchedules[0].SessionId)
    @Html.Hidden("hall", eventInfo.Key.EventSchedules[0].HallId)
    @Html.Hidden("client", eventInfo.Key.EventSchedules[0].BasePlace.PremieraClientId)
    <button type="submit">Make</button>
}
like image 200
Darin Dimitrov Avatar answered Oct 04 '22 23:10

Darin Dimitrov