Goal:
Display the url address as "http://localhost:49332/Home/Contact2/1?idd=first" (except "http://localhost:49332") instead of "http://localhost:49332/Home/Contact2/1?one=second&two=yes"(except "http://localhost:49332") when you have pressed the button Create
Problem:
I don't know how to do it? I strongly believe that it has to do with querystring.
Info:
*I would like the querystring to take place inside of the method "Public ActionResult contact2(int? id, string one, string two)"
*You can download the source code on website (https://drive.google.com/file/d/0B23pYZkpAyafbEtYTjhYaHhtNkk/view).
I believe that in your case, the easiest way to do this is using the AttributeRouting.
http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx
Or else, you should configure the routes inside RoutConfig class, this link might be helpful https://msdn.microsoft.com/en-us/library/cc668201%28v=vs.140%29.aspx
If you want to use only querystring, just put the action parameter name in the url... everything should work fine.
Related
Currently, our application has a lot of ActionLinks that only pass an id to the controller, where the object is retrieved again (that was already present/known on the previous page). Is there any cleaner way to do this, like pass the object instead? As our backend isn't too quick it takes a few seconds to load in the popup which you get after you click on the ActionLink.
I've seen some solutions using ajax to post the object but that doesn't seem like the neatest solution to paste those js lines under/at every ActionLink that is used in combination with a popup.
The #Html.ActionLink() method will generate a url link to the given Controller/Action. Thus, it can only contain parameters that can be contained in the url of the link. So you cannot pass an object through on the url.
If you need to pass through the reference to an object that is stored on the server, then try setting a parameter of the link to give a reference to the object stored on the server, that can then be retrieved by the action (example, the Id of the menuItem in question).
Parameters in the ActionLink are set through the collection that you passed in as the third item in your function call above. Assuming default routing, this would give an address that looks like /Admin/EditPage/?name=XXX where XXX is the value of menuitem.Title. If you included something else here like itemId = menuitem.Id then it would add this as a query string parameter to the url generated, which would then be accessible to the action that is the target of this link.
I want my webpage to be multilingual. That's why I created a Button that changes a session variable to for example fr (French). It's accomplished by an action result that changes the session variable. Now I am not sure what the action result should return so I am going to get to the same page as I was before. Can you help me out?
You can't, not how you describe.
What you can do is set the language as a parameter in your query, either as a regular query parameter (http://example.com/page?lang=kr) or using URL rewriting (http://example.com/kr/page) and use the parameter in the view to render the correct language.
And when you get to writing the language changes links, you use an action link like normal, but you overwrite the language parameter.
Way 1 : Use same concept of return url using query string that used to redirect user back to the same page after login
Way 2 : use ajax request to change language and on success just make reload
Way 3 : add language to url
Actually i found a way to do it myself, if I use this in my ActionResult the page refreshes and my language is changed.
return Redirect(HttpContext.Request.UrlReferrer.AbsoluteUri);
By using this you just reload the page that was loaded before, so when the variable for the language has been changed by my action controller it pulls the right textes out of my Database and by reloading it uses them.
I'm creating a custom tree inside umbraco7 and and have a button which will call an UmbracoAuthorizedApiController called ExportApiController i added a constructor and set a breakpoint on it to see if this controller is indeed instanced and it is. But when i try to call the call my action it is returning an 405 (Method Not Allowed)
And in the response i see the following text
{"Message":"The requested resource does not support http method 'GET'."}
above my controller action i have the HttpGet attribute and i also tried the put or post but nothing helps. But i think that when i use post i need to do something else inside Angular to call it using a submit on a form? But i'm not sure how or why.
If you need more information please tell me and i will post it.
Thanks.
I had the same problem and for me the problem was that I accidently had HttpGetAttribute.HttpGet instead of System.Web.Http.HttpGet.
What is the name of your method ? I find that if you don't actually have "get" in the front of the name it would throw out this error so
public string CorrectTime(string time) < -- would fail but
public string GetCorrectTime(string time) <-- would pass
Because you want to export data, I guess you are trying to use this controller for the big public. However, UmbracoAuthorizedApiController are only for back-end users as mentioned in the documentation. You should not use these to expose data outside the Umbraco back-end.
If you want to expose data, and you want to validate against the members (not users) you should prefix your UmbracoApiController with the MemberAuthorize attribute
Not sure if this is possible but I need my action to bind to a query string parameter with a full stop in the name. Is this possible?
For example my query string is
http://foo.com?foo.bar=test
so is there anything I can put in my method declaration so I can pick it up?
public JsonResult TestAction(string foo.bar)
(I know this wont work)
I know I can get the value from the Request object but I'd rather not do that.
Thanks in advance
You can use the Bind attribute:
public JsonResult TestAction([Bind(Prefix="foo.bar")]string foobar)
For instance if I have http://www.mywebsite.com/about.aspx. Store about.aspx (or whatever page we're on) in a variable. Also need this to work even if there is information after the page in the url such as a query string.
Ah - I suggest you read the ASP.NET documentation VERY carefully. The HttpRequest object, available as Request property contains all information that you can have about the request. This includes the path called, all parameters etc.
Weighing in a little late, but others may get here as I did.
You could try to use a combination of the Uri and Path objects. For example:
Uri test = new Uri("http://www.microsoft.com/default.aspx?id=7&em=958");
Console.WriteLine(Path.GetFileName(test.AbsolutePath));
Not sure if this is the "proper" way but it works for me.