Ambigous Routes Using ASP.Net MVC - asp.net

So far I still have the standard routing. What I tried to do is
public Foo : Controller
{
public ActionResult Index(int id)
{
return View("List", repo.GetForId(id));
}
public ActionResult Index()
{
return View("List", repo.GetAll());
}
}
The URL I entered was
localhost/Foo/Index.
I was under the asumption that it was smart enough to figure out which method I wanted. All I get though is an error telling me that the call is ambigous. From reading the route tutorial I thought that this would work. What am I missing?
Sorry:
Duplicate. I am voting to close.

Method overloading (two actions with the same name) resolution is based on the HTTP verb. This means that if you want to have two actions with the same name you need to differentiate them by the HTTP verb they accept:
public ActionResult Index(int id)
{
return View("List", repo.GetForId(id));
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index()
{
return View("List", repo.GetAll());
}
Thus when you call GET /Home/Index the first action will be invoked and when you call POST /Home/Index the second one will be invoked.
Also note that you should probably use nullable integer as the id argument because if no parameter is passed in the request, the model binder will fail.
A RESTful way to handle this, following conventions that I would suggest you is this:
public ActionResult Index()
{
return View(repo.GetAll());
}
public ActionResult Show(int id)
{
return View(repo.GetForId(id));
}
which will allow you to handle both /Home/Index and /Home/Show/10 requests. You probably will have different views as well because the first one will be strongly typed to IEnumerable<T> while the second one to T.

Related

What's the difference between [HttpGet] and [HttpGet("{id}")]?

What are the differences between HTTPGET method and HTTPGET("{id}") method?
What is the API method that is used to update table columns?
[HttpGet]
public IActionResult Get()
{
return new JsonResult(users);
}
// GET api/values/5
[HttpGet("{id}")]
public IActionResult Get(string id)
{
return new JsonResult(response);
}
You should take a look at Attribute Routing in Web API.
The first method is routing to a clean api:
/api/controller
The second specifies route value in the attribute and will be called via the following url:
/api/controller/5
The second one is normally used to update existing queries, as you specify specific value in the route whilst the first specifies nothing.

IActionResult vs ObjectResult vs JsonResult in ASP.NET Core API

What's the best option to use in a Web API that will return both HTTP status codes and JSON results?
I've always used IActionResult but it was always a Web App with a Web API. This time it's only Web API.
I have the following simple method that's giving me an error that reads:
Cannot implicitly convert type Microsoft.AspNetCore.Mvc.OkObjectResult
to System.Threading.Tasks.Task Microsoft.AspNetCore.Mvc.IActionResult
[HttpGet]
public Task<IActionResult> Get()
{
return Ok();
}
Return the object that best suits the needs of the request. As for the action's method definition, define it with IActionResult to allow the flexibility of using an abstraction as apposed to tightly coupled concretions.
[HttpGet]
public IActionResult Get() {
return Ok();
}
The above action would return 200 OK response when called.
[HttpGet]
public IActionResult Get() {
var model = SomeMethod();
return Ok(model);
}
The above would return 200 OK response with content. The difference being that it allows content negotiation because it was not specifically restricted to JSON.
[HttpGet]
public IActionResult Get() {
var model = SomeMethod();
return Json(model);
}
The above will only return Json content type.
A very good article to read on the subject
Asp.Net Core Action Results Explained

A multi-stage model replenishment

I have a need, to make a multi-step process in which my, let's say "big model" will be updated every step and in the end saved to a database. For now, I made it this way:
[HttpGet]
public ActionResult StepOne()
{
return View();
}
[HttpPost]
public ActionResult StepOne(StepOneViewModel viewModel)
{
return RedirectToAction("StepTwo", "SameController", viewModel);
}
[HttpGet]
public ActionResult StepTwo(StepOneViewModel stepOneViewModel)
{
var stepTwoViewModel = new StepTwoViewModel()
{
stepOneViewModel = stepOneViewModel
};
return View(stepTwoViewModel );
}
[HttpPost]
public ActionResult StepTwo(StepTwoViewModel viewModel)
{
return RedirectToAction("StepThree", "SameController", viewModel);
}
... And so on. This approach seems to be good because it's strongly typed in a view, and I can have a separate validation for every "smaller" view model. On the other hand, I have to pass every parameter of the previous view model via Hidden inputs (making a partial for example, not to mess the code too much).
But is there another, better way to do so ? Is it possible to pass the whole object with many properties to the hidden or something similar ?

Difference Between ViewResult() and ActionResult()

What is the difference between ViewResult() and ActionResult() in ASP.NET MVC?
public ViewResult Index()
{
return View();
}
public ActionResult Index()
{
return View();
}
ActionResult is an abstract class that can have several subtypes.
ActionResult Subtypes
ViewResult - Renders a specifed view to the response stream
PartialViewResult - Renders a specifed partial view to the response stream
EmptyResult - An empty response is returned
RedirectResult - Performs an HTTP redirection to a specifed URL
RedirectToRouteResult - Performs an HTTP redirection to a URL that is determined by the
routing engine, based on given route data
JsonResult - Serializes a given ViewData object to JSON format
JavaScriptResult - Returns a piece of JavaScript code that can be executed on the client
ContentResult - Writes content to the response stream without requiring a view
FileContentResult - Returns a file to the client
FileStreamResult - Returns a file to the client, which is provided by a Stream
FilePathResult - Returns a file to the client
Resources
What's the difference between ActionResult and ViewResult for action method? [ASP.NET Forums]
ActionResult is an abstract class.
ViewResult derives from ActionResult. Other derived classes include JsonResult and PartialViewResult.
You declare it this way so you can take advantage of polymorphism and return different types in the same method.
e.g:
public ActionResult Foo()
{
if (someCondition)
return View(); // returns ViewResult
else
return Json(); // returns JsonResult
}
It's for the same reason you don't write every method of every class to return "object". You should be as specific as you can. This is especially valuable if you're planning to write unit tests. No more testing return types and/or casting the result.
ViewResult is a subclass of ActionResult. The View method returns a ViewResult. So really these two code snippets do the exact same thing. The only difference is that with the ActionResult one, your controller isn't promising to return a view - you could change the method body to conditionally return a RedirectResult or something else without changing the method definition.
While other answers have noted the differences correctly, note that if you are in fact returning a ViewResult only it is better to return the more specific type rather than the base ActionResult type. An obvious exception to this principle is when your method returns multiple types deriving from ActionResult.
For a full discussion of the reasons behind this principle please see the related discussion here: Must ASP.NET MVC Controller Methods Return ActionResult?
In the Controller , one could use the below syntax
public ViewResult EditEmployee() {
return View();
}
public ActionResult EditEmployee() {
return View();
}
In the above example , only the return type varies . one returns ViewResult whereas the other one returns ActionResult.
ActionResult is an abstract class . It can accept:
ViewResult , PartialViewResult, EmptyResult , RedirectResult , RedirectToRouteResult , JsonResult , JavaScriptResult , ContentResult, FileContentResult , FileStreamResult , FilePathResult etc.
The ViewResult is a subclass of ActionResult.
In Controller i have specified the below code with ActionResult which is a base class that can have 11 subtypes in MVC like:
ViewResult, PartialViewResult, EmptyResult, RedirectResult,
RedirectToRouteResult, JsonResult, JavaScriptResult, ContentResult,
FileContentResult, FileStreamResult, FilePathResult.
public ActionResult Index()
{
if (HttpContext.Session["LoggedInUser"] == null)
{
return RedirectToAction("Login", "Home");
}
else
{
return View(); // returns ViewResult
}
}
//More Examples
[HttpPost]
public ActionResult Index(string Name)
{
ViewBag.Message = "Hello";
return Redirect("Account/Login"); //returns RedirectResult
}
[HttpPost]
public ActionResult Index(string Name)
{
return RedirectToRoute("RouteName"); // returns RedirectToRouteResult
}
Likewise we can return all these 11 subtypes by using ActionResult() without specifying every subtype method explicitly.
ActionResult is the best thing if you are returning different types of views.
To save you some time here is the answer from a link in a previous answer at https://forums.asp.net/t/1448398.aspx
ActionResult is an abstract class, and it's base class for ViewResult class.
In MVC framework, it uses ActionResult class to reference the object your action method returns. And invokes ExecuteResult method on it.
And ViewResult is an implementation for this abstract class. It will try to find a view page (usually aspx page) in some predefined paths(/views/controllername/, /views/shared/, etc) by the given view name.
It's usually a good practice to have your method return a more specific class. So if you are sure that your action method will return some view page, you can use ViewResult. But if your action method may have different behavior, like either render a view or perform a redirection. You can use the more general base class ActionResult as the return type.

Altering the ASP.NET MVC 2 ActionResult on HTTP post

I want to do some processing on a attribute before returning the view. If I set the appModel.Markup returned in the HttpPost ActionResult method below to "modified" it still says "original" on the form. Why cant I modify my attribute in a HttpGet ActionResult method?
[HttpGet]
public ActionResult Index()
{
return View(new MyModel
{
Markup = "original"
});
}
[HttpPost]
public ActionResult Index(MyModel appModel)
{
return View(new MyModel
{
Markup = "modified"
});
}
Because "original" is stored in ModelState. When form values are collected on MVC side, they are stored in ModelState object. You propably used Html.TextBox helper. When you recreate view after POST, it looks up into ModelState first and if there is posted value, it sets this value. Value in model object doesn't count anymore.
One of the solutions is to follow POST-REDIRECT-GET pattern. First POST, do something with data and then redirect:
[HttpPost]
public ActionResult Index(MyModel appModel)
{
//do something with data
return RedirectToAction("Index");
}
If you want to pass something between redirects, you can use TempData:
[HttpPost]
public ActionResult Index(MyModel appModel)
{
//do something with data
TempData["Something"] = "Hello";
return RedirectToAction("Index");
}
[HttpGet]
public ActionResult Index()
{
var something = TempData["Something"]; //after redirection it contains "Hello"
}
After redirect, ModelState is gone, so the is no value to override. POST-REDIRECT-GET pattern also helps to get rid of form reposting effect when you press F5 in browser.

Resources