Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

asp.net mvc - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...