How do you extract parameters from an MVC URL - asp.net-mvc

I have a URL route of "ticket/{ticket_guid}/{controller}/{action}/{id}". Normally, the parameters would be parsed for you and passed as parameters to the controller action. I need to get access to the {ticket_guid} parameter from inside the Global.asax's Application_AuthenticateRequest event handler. What is the best way to parse out the ticket_guid parameter?

I think you're looking for something like this (if I have correctly assumed ASP.Net MVC):
ViewContext.RouteData.Values["ticket_guid"].ToString()

Related

How to keep a querystring parameter in subsequent requests in ASP.NET MVC 5?

In my ASP.NET MVC 5 application I want to use some parameters to "survive" several subsequent requests irrespective of whether they were processed in controller or not.
For example, if the request has order/add/1234?promocode=new2014 in it, I want it to stay all the way the user puts together his or her order until he proceeds to check-out. This means, that every action link generated using HTML helpers in my views should contain this parameter.
How can I achieve this?
You could save this parameter in Viewbag and then use it again like
#Url.Action("index",new {promocode = Viewbag.CurrentPromocode})

How to access "main" querystring from ActionMethod called from Partial

In an MVC project, I have a partial
#Html.Partial("_MyPartial");
which calls a controller method (using AJAX with jQuery) via URL
'/Home/MyActionMethod?type=myType&sort=az&page=1'
The Home controller contains MyActionMethod, which returns a PartialViewResult.
MyActionMethod has parameters matching those in the URL, which are mapped as per usual.
My question is how to access the top level querystring from MyActionMethod, because I want to include extra values in there? By top level I mean the one seen in the browser, as opposed to the one sent to MyActionMethod from _MyPartial
Many thanks
You have to include them as query string parameters when performing your AJAX call. So when building the link on the server, read the query string parameters and append them to those that you have already.

Parameters across different controller in webflow -Grails

I am looking at a way to access parameter passed from different Url to use in my web Flow. I am getting URl like
----/newUserRegistration/userRegistration?execution=e20s1&tkn=f9e1cfe077c75ec79f39c61543407cac96ae57e2eca6576e5312b2a266cd8df0. New user registration is my controller name and userregistration is flow name. I have to capture tkn parameter and use this for my flow. How do I do this ? Please suggest me on this....
Given that your Webflow DSL is correct in an action block you can access it with params.tkn.

Silverlight and MVC: post object to controller method

I have an MVC project in which a controller action returns some JSON data (i.e. via /Home/GetData URL). This action also takes a custom object as a param.
This signature for the action is JsonResult GetData (MyCustomObject o)
I also have a client Silverlight project in which I'm constructing MyCustomObject and trying to call this URL (/Home/GetData/) via HttpWebRequest. However, I'm having trouble figuring out how to post in my object in this call. Do I need to serialize it to Json in order to pass it in?
Thanks so much!
MVC can accept and bind the submitted data to your MyCustomObject object, regardless of whether it is submitted as JSON, XML, a query string, a standard form POST, etc.
MVC does not require the object to be submitted in a particular fashion. That is up to you as the designer to determine what works best under the particular circumstances, given all of your requirements.
When submitted, MVC will use the ValueProvider suitable to the form of the data submitted, and the DefaultModelBinder will attempt to use the values in the ValueProvider to bind to your model.
Thanks for your help! Since the web app handles this with a getJSON call, I ended posting the object as a query string param i.e. I'm making a web request to http://../controller/action/view.aspx?custObject.property1=<value>&custObject.property2=<value> etc

ASP.Net MVC redirecttoaction not passing action name in url

I have a simple create action to receive post form data, save to db and redirect to list view.
The problem is, after redirecttoaction result excutes, the url on my browser lost the action section. Which it should be "http://{hotsname}/Product/List" but comes out as "http://{hotsname}/Product/".
Below is my code:
[HttpPost]
public ActionResult Create(VEmployee model, FormCollection fc)
{
var facility = FacilityFactory.GetEmployeeFacility();
var avatar = Request.Files["Avatar"].InputStream;
var newModel = facility.Save(model, avatar);
return RedirectToAction("List");
}
The page can correctly render list view content, but since some links in this view page use relative url, the functions are interrupted. I am now using return Redirect("/Employee/List") to force the url. But I just wonder why the action name is missing. I use MVC3 and .Net framwork 4.
I am new to ASP.Net MVC, thanks for help.
Your route table definitely says that "List" action is default, so when you redirect to it as RedirectToAction("List") - routing ommits the action because it is default.
Now if you remove the default value from your routes - RedirectToAction will produce a correct (for your case) Url, but you'll have to double check elsewhere that you are not relying on List being a default action.
Well, Chris,
If you get the right content on http://{hotsname}/Product/ then it seems that routing make that URL point to List either indirectly (using pattern like {controller}/{action}) and something wrong happens when resolving URL from route or {action} parameter is just set wth default value List. Both URLs can point to the same action but the routing engine somehow takes the route without explicit action name.
You should check:
Order in which you define your routes
How many routes can possibly lead to EmployeeController.List()
Which one of those routes has the most priority
Default values for your routes
Just make the route with explicit values: employee/list to point to your List action and make sure that is the route to select when generating links (it should be most specific route if possible).
It would be nice if you provide your routes mappings here.
but since some links in this view
page use relative url, the functions
are interrupted.
Why do you make it that way? Why not generate all the links through routing engine?
When using the overload RedirectToAction("Action") you need to be specifying an action that is in the same controller. Since you are calling an action in a different controller, you need to specify the action with the alternate overload e.g. RedirectToAction("List", "Employee").

Resources