how do get header that i set in response object - c#

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"

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.

get ajax posted Data From Request body

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();
}

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

Adjust parameter name in controller action to map HTTP request parameter

I have the problem that I must map a fixed URL parameter that contains an underline (e.g. purchase_id) to an controller.
public ActionResult Index(
long purchase_Id,
That is working, and it's not my problem. The thing that annoys me, is the underline in the parameter name, due to the fact, that I can't change the given URL Parameter. It's called purchase_id
e.g. http://www.example.com/Order?purchase_id=12123
Is there any chance to get the following thing working, without changing the URL parameter?
public ActionResult Index(
long purchaseId,
Thanks for your help.
public ActionResult Index()
{
string purchaseId = Request["purchase_id"];
return View();
}
or:
public ActionResult Index([Bind(Prefix="purchase_id")]string purchaseId)
{
return View();
}

Spitting out a Index view using parameters

I'm using ASP.MVC and trying to learn...
I have the following controller
// get all authors
public ActionResult Index()
{
var autores = autorRepository.FindAllAutores();
return View("Index", autores);
}
// get authors by type
public ActionResult Index(int id)
{
var autores = autorRepository.FindAllAutoresPorTipo(id);
return View("Index", autores);
}
If I try http://server/Autor/1 I get a 404 error. Why is that?
I even tried to create a specific method ListByType(int id) and the correspondent view, but that does not work too (URL: http://server/Autor/ListByType/1)
Any ideas?
EDIT Oh, the http://server/Autor works just fine. The method without parameters is spitting out my view correctly.
Assuming your class is called AutorController, and assuming you have the default route configuration of
{controller}/{action}/{id}
You should be able to request
/Autor/Index/<anything>
However, you seem to be a bit confused on the action methods. You could combine your action methods like so:
public ActionResult Index(int? id)
{
var autores; // I know this wont compile - but without knowing what type FindAllAutoRes returns, I can't make a specific type for this example
if(id.HasValue)
autores = autorRepository.FindAllAutoresPorTipo(id);
else
autores = autorRepository.FindAllAutores();
return View(autores); // Will automatically select the 'Index' View
}
MVC will select the first valid action method that corresponds to your route data - so if you request /Autor/Index/3, you will get the first action method, but since it has no parameters, the id route data is not bound to anything.

Categories

Resources