get ajax posted Data From Request body - c#

How to get these ajax posted data from request body ?
search=title&searchType=department+1&X-Requested-With=XMLHttpRequest

You can use [FromBody] attribute to the parameter that you need to get from the body.
Public ActionResult ([FromBody] string param1)
{
return View();
}
Hope this would help.

You can simply get QueryString paramaters as arguments in your Controller:
Public ActionResult Index(string search, string searchType,string XRequestedWith)
{
return View();
}
Of course this way your paramater can not have dashes like X-Requested-With
You can also use Request.QueryString to get QueryString. This way you won't have the restriction above.

If you want to get those ajax posted data from request body the first instruction is not to put those values in query string put those values in form body.Better it would be if you create a modal and use those property to pass in ajax.
Now you would fetch those value in action method like
Public ActionResult YourMethodName([FromBody]YourModel objYourModel)
{
// use objYourModel to fetch your values
return View();
}

Related

Why Should we assign a Http Attribute to every action in controller

I having a question should we assign a http attribute for each action?
Like for example Index Page that doesn't have any action just displaying html we still need to assign a http attribute? Why since there is no retrieving data.
And If I remove Http Attribute for ViewDetail and CreateRecord, the page is still working and no bug what the huge difference for adding and not adding http attribute
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult ViewDetail()
{
//.... Get Data Action
return Redirect(Url.Action("Edit","Home"));
}
[HttpPost]
public ActionResult CreateRecord()
{
//.... Create Action
return Redirect(Url.Action("Edit","Home"));
}
There may be some methods in your Controller class that are not HTTP Endpoints.

how do get header that i set in response object

My action method returns a view. It looks like the following.
public public ActionResult Init(string additionalParams)
{
Response.AddHeader("additionalParams", additionalParams);
return View(model);
}
The view has a form. The form is submitted to another action method
[HttpPost]
public ActionResult InitPost(MyModel model)
{
string additionalParams = Request.Headers.Get("additionalParams"); <--- getting null
}
how do i get the additionalParams in the post? Please help.
So on the Init(), I was using Route attribute routing, that makes urls look like this http://wwww.example.com/23/22/... Apparently this caused issues with the length of url. So I switched to using query string, by removing the route attribute routing. Now with this url, I can pass my additionalParams just fine, http://www.example.com?id=222&otherID=222&additionalParams="whatever"

How do I create a GET form with validation in MVC

I'm having a real problem trying to articulate this seemly simple problem.
I have a single view that contains a FORM with a few search fields at the top of the view and the results of that search get shown on the same view after submitting the form.
I have a single HTTPGET controller method that takes the form fields as parameters and IF it was submitted by a user it will pass the model back to the view with the results to be shown and pre-populate the search form with what they filled out.
How can I tell if the page was loaded with default parameters vs. someone actually submitted the form.
What's the best way to accomplish this?
If I am understanding your question correctly then I think you need to consider the HttpGet attribute:
https://msdn.microsoft.com/en-us/library/system.web.mvc.httpgetattribute(v=vs.118).aspx
and the HttpPost attribute:
https://msdn.microsoft.com/en-us/library/system.web.mvc.httppostattribute(v=vs.118).aspx
Lets say you have a create method. The Http method would look like this:
[HttpGet]
public ActionResult Create()
{
}
and the post method would look like this:
[HttpPost]
public ActionResult Create(Person p)
{
//Logic to insert p into database. Could call an application service/repository to do this
}
RedirectToAction solve the problem.
you can go back to the get method after submited data and populate the view with default values
[HttpGet]
public ActionResult Create()
{
// fill model to default data
return view(model);
}
[HttpPost]
public ActionResult Create(Person p)
{
//do your stuff save data
return RedirectToAction("Create");
}
or
[HttpPost]
public ActionResult Create(Person p)
{
if(...)
{
//do your stuff any logic
return RedirectToAction("Create");
}
//do your stuff
return view(...);
}

Is it possible to create a parameter binding on both FromURI and FromBody?

I looked up up the documentation for ASP.NET Web API parameter binding, they seem to have either fromURI and fromBody only. Is it possible to do both?
Here is some background info. I'm creating a webhook receiver/handler, where I have control over which URL is the webhook, but I do not have control over what the payload will be like until later stage of the workflow, so I need to take it in as JSON string first.
My hope is to be able to set up the route that can take in querystring and also Json string payload from HTTP POST. For example .../api/incoming?source=A.
If I understand correctly you're trying to use both the Post data from the body and some parameters from the URI. The example below should capture your "source=a" value from the queryString.
[Route("incoming")]
[HttpPost]
public IHttpActionResult Test([FromBody] string data, string source)
{
//Do something
return Ok("my return value");
}
Or you could use as below if you formatted your route as .../api/incoming/source/A.
[Route("incoming/{source:string}")]
[HttpPost]
public IHttpActionResult Test([FromBody] string data, string source)
{
//Do something
return Ok("my return value");
}

Html.ActionLink on Post Methods

Is there any way that I can use a html.actionLink to post data?
public ActionResult Test()
{
return View();
}
[HttpPost]
public ActionResult Test(TestModel test)
{
return View(test);
}
and on my view...
#Html.ActionLink("ClickMe","Test", new {Test1 = "actionLink", Test2 = "actionLinkDidThis"}, FormMethod.Post)
Is there any way that this action link could get in the httpPost method and not the httpGet method?
This will render an anchor tag which will produce a get request since it's a regular hyperlink at the end of the day.
You can Make the action method a Get method though by putting a HttpGet attribute on it instead. The MVC model binder will be able to deserialize the Get request into the TestModel object for Get requests as well.
I would use a button and do a form post if you need a post request

Categories

Resources