Adjust parameter name in controller action to map HTTP request parameter - c#

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

Related

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"

.net core not routing to POST method

Link to project here -> https://github.com/crumdev/hqbbq.git
I have a simple 3 page MVC website with 1 controller. For the contact page I have a small 3 input form. Below are the Controller methods:
[Route("contact/")]
public IActionResult Contact()
{
return View();
}
[HttpPost]
public IActionResult Contact(string name, string email, string message)
{
ViewBag.Name = name;
ViewBag.Email = email;
ViewBag.Message = message;
return View();
}
When I submit the form with a breakpoint inside of the Contact method for HttpPost it never breaks but instead uses the regular method and just returns the view again. I have tried reducing my form to just the name field and only capture that and it does not enter the POST method. I have given the regular Contact method the [HttpGet] attribute so it can only be used strictly for GET requests and when I submit the form then it bypasses my controller altogether and returns the Dev exception page that is blank except for "Hello World!" on the screen. I have read through the documentation and followed tutorials on teamtreehouse.com for regular ASP.Net as well but cannot understand why it is behaving this way.
Edit:
Here is the code for the page submitting the POST. I am just using a plain HTML form with the POST method for submitting the data.
https://github.com/crumdev/hqbbq/blob/master/HQ-BBQ/Views/Home/contact.cshtml
The POST action needs to have a route as well if the intention is to use attribute routing.
[HttpGet]
[Route("contact")]
public IActionResult Contact() {
return View();
}
[HttpPost]
[Route("contact")]
public IActionResult Contact(string name, string email, string message) {
ViewBag.Name = name;
ViewBag.Email = email;
ViewBag.Message = message;
return View();
}
Note the exclusion of the slashes as they are not needed. Make sure the names and ids of the form inputs match the parameters of the target action
It looks like you're missing the Route attribute on the [HttpPost] method.
Try this.
[HttpPost]
[Route("contact/")]
public IActionResult Contact(string name, string email, string message)
Also update your view code, so that the name property of your <input /> tags matches the arguments of your controller action.
Remember that MVC uses the name property to bind to the arguments in your controller action. MSDN Model Binding
For example update your email input to include the name property:
<input name="email" id="email" class="input" type="text" placeholder="Email" value="#ViewBag.Email">
You'll also need to update the text area name to name="message".
You can use the FromForm annotation for your parameters in the controller post method
[HttpPost]
[Route("contact")]
public IActionResult Contact([FromForm]string name, [FromForm]string email, [FromForm]string message)
I would also recommend to use a viewmodel rather than passing all the fields of your form as parameters. Imagine you have a form with 10 fields, your method signature would be a way harder to read
I couldn't route to action because i passed object not matched to model, for instance
public class NewsModel : IActive
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long Id { get; set; }
public string NewsName { get; set; }
public string NewsText { get; set; }
public bool Active { get; set; }}
{"id":0,"newsName":"123","newsText":"123","active":""}
active - should be boolean, but i passed string.
So first thing one have to do is to remove parameters in Post methods.
[HttpPost] public async Task<IActionResult> PostNews( )
If now you get in action, so you have problem in model, otherwise - you have problem in routing

RedirectToAction with a parameter not working

i am trying to redirect to another action within the same controller
action is called index
[HttpGet]
public ActionResult Search(string city)
{
return RedirectToAction("Index", "Rentals", new { CityName = city });
}
this is index action
[HttpPost]
public ActionResult Index(String CityName)
{
}
am i missing something?
You are trying to redirect action which is searching for a matching action but in this case there is no get action, so you have to add a get method to accept redirect. If you want, you can check the HTTPGET or POST inside the method
[HttpPost]<---- Remove this
public ActionResult Index(String CityName)
{
}
Please change HttpPost to HttpGet
[HttpGet]
public ActionResult Index(String CityName)
{
}
Because whenever you call the Action, then the GET method will be first called.
As the two actions are in the same controller, you could call the Index method directly from Search like this:
return Index(city);
not necessarily to user the RedirectToAction method.

how to override a action method in the controller with same name and signature.?

public ActionResult ListRss(int languageId)
{
return View();
}
public ActionResult ListRss(int languageId)
{
return View() ;
}
I have this two method, Here I just want to override first method with the second one. My goal is to just keep the first method unused for the reference and make the another method.
Is it Possible any way..?
as names of the actions are very strongly binded with the request params and routing mechanism.. you simply can not do much as far as i know.. ofcourse you can differentiate them by attributes of which type of request is going to made...
[HttpPost]
public ActionResult ListRss(int languageId)
{
return View();
}
[HttpGet] // or any other http types
public ActionResult ListRss(int languageId)
{
return View() ;
}
other way which is more complicated is to create own controller factory..
You can't have methods with the same name and signature in one class. You have to rename one of them if the signature is the same.
If you don't want to use the first method, maybe you should just comment it out and keep it as reference in comments.

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