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.
Related
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"
I am writing an ASP.NET Core API and I was wondering how I could return a View in a Controller. The goal is to provide a documentation on this page.
What I have tried is to create a Controller class, that returns a ViewResult, like below:
[Route("api/[controller]")]
public class HomeController : Controller
{
[HttpGet]
public IActionResult Get()
{
return View();
}
}
Then, I have Created a simple view named Index.cshtml in a View/Home repository.
When I launch the app with /api/home, it does return an Internal Server Error (as logged in the JS console of the browser). Although, if I return a random ObjectResult like so:
[HttpGet]
public IActionResult Get()
{
return new ObjectResult(new {bonjour = 1});
}
It does return the correct data model.
Does anyone have an idea on how return a View using ASP.NET Core API Tools ?
You need to specify full path to your view, because your action name is Get not Index
Try this
[HttpGet]
public IActionResult Get()
{
return View("~/Views/Home/Index.cshtml");
}
I think if you define a route on the class you also need to do this for any of the ActionResults so try adding the below:
[HttpGet("nameOfThisRoute")]
[Route("nameOfRoute/Get")]
public IActionResult Get()
{
return View();
}
so your Url will be "api/nameOfRoute/Get"
More Info: http://www.ryadel.com/en/custom-routing-method-names-in-asp-net-5-mvc-6/
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
I am attempting to make a simple test website to allow me to list, create, edit and delete customer objects using MVC4.
Inside my controller I have 2 create methods, a Get for when the form loads with the controls, and a Post that actually saves the data.
//
// GET: /Customer/Create
[HttpGet]
public ActionResult Create()
{
return View();
}
//
// POST: /Customer/Create
[HttpPost]
public ActionResult Create(Customer cust)
{
if (ModelState.IsValid)
{
_repository.Add(cust);
return RedirectToAction("GetAllCustomers");
}
return View(cust);
}
However when I run the project and attempt to access the create action I get an error that:
The current request for action 'Create' on controller type 'CustomerController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Create() on type [Project].Controllers.CustomerController
System.Web.Mvc.ActionResult Create([Project].Models.Customer) on type [Project].Controllers.CustomerController
My I understand that it can't see the difference between my Get and Post methods, but I have added the attribues. What could be the cause of this and how can I make it work again?
MVC does not authorize you to have 2 action methods with the same name.
BUT you can have 2 action methods with the same URI when the http verb differs (GET, POST). Use the ActionName attribute to set the action name. Don't use the same methods names. You can use any name. A convention is to add the http verb as the method suffix.
[HttpPost]
[ActionName("Create")]
public ActionResult CreatePost(Customer cust)
{
if (ModelState.IsValid)
{
_repository.Add(cust);
return RedirectToAction("GetAllCustomers");
}
return View(cust);
}
I have two actions in a controller:
public ActionResult ReportRequest()
{
return View();
}
[HttpPost]
public ActionResult Report(ReportRequestObj rr, FormCollection formValues)
{
if (Request.HttpMethod != "POST")
return RedirectToAction("ReportRequest");
ReportingEngine re = new ReportingEngine();
Report report = re.GetReport(rr);
return View(report);
}
My problem is, the URL for the 'Report' page gets saved in the browser, and when I click on it, I get a 404, because the request has not been posted.
I put in a handler in to redirect to the report request page however on debugging it just doesn't seem to hit this at all.
Is there any other way I can determine if the request is a post, and if not redirect back to another page?
Thanks
Add action
public ActionResult Report()
{
return RedirectToAction("ReportRequest");
}
or just remove [HttpPost] from action Report
You cannot handle 404 error here because that request will never arrive to your action. You are using an action filter that makes sure that only POST requests arrive to that piece of code.
You should create another action method to respond to GET requests and return your view inside that.
[HttpGet]
public ActionResult Report()
{
return View("ReportRequest");
}