MVC 4 is overwriting specific Action-Parameters - asp.net-mvc-4

MVC 4 does present me some strange behaviour at the moment.
Imagine the following Code:
TestController.cs
public class TestController : Controller
{
public ActionResult Index(Function function, string action)
{
return View();
}
public class Function
{
public string Action { get; set; }
}
}
It seems, that when I call the URL directly through the browser (localhost:PORT/Test), the Action-Property gets automatically filled with "Index".
If the Action would be named "MySuperDuperActionWhichGetsInvokedQuiteOften", exactly this Methodname would be in the property.
Can somebody explain what MVC is doing here?
The Problem is, that I of course want to fill that stuff myself, for example through an AJAX-Query. But if MVC is filling in this property all by itself, this breaks some behaviour.
I could, of course, just rename my property and it would be working, but it would still be quite interesting what's going on.
EDIT
I would understand it that my second parameter, string action, get's filled with the method-name. But why on earth would MVC bind any property/parameter that is named the same to the request-value of it?

It is problem with default model binder. It "maps" request fields to properties in your class. There is an article of MSDN describing how does it works but to simply this situation the code will be like this:
Action = Request["action"] //where of course Request["action"] equals to name of your action

Related

Where does this "Required" attribute for the Swagger UI coming from for ASP.NET?

I have the following in my ASP.NET Controller:
[Produces("application/json")]
[Consumes("application/json")]
[Route("api/[controller]")]
[ApiController]
public class ConnectionManagersController : ControllerBase
{
[HttpGet("{connectionManagerID:int}")]
[ProducesResponseType(typeof(ConnectionManagerModel), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ConnectionManagerModel>> GetConnectionManagerAsync(int connectionManagerdID){}
}
However when I run the app and the Swagger UI comes up I get the following screen:
There are TWO connectionManagerID fields on the Swagger UI - the first is an int (which is should be) and the second is a string and is required which I dont know where that is coming from.
I dont know where the *required field is coming from.
The parameter connectionManagerdID is an int - it is not nullable so in effect it is required.
Try this:
public async Task<ActionResult<ConnectionManagerModel>> GetConnectionManagerAsync(int connectionManagerdID = null){}
It should set it to optional.
it is from here, you will probably get 404, if you don't supply id to url.
[HttpGet("{connectionManagerID:int}")]
and if you don't want to be required try this
[HttpGet("{connectionManagerID?:int}")]
in this case maybe it makes sense to change the action too
public async Task<ActionResult<ConnectionManagerModel>> GetConnectionManagerAsync(int? connectionManagerdID){}
or you can keep what you have. It will be 0 by default.
I had the two parameters spelled differently. :(

asp.net core api - how to distinguish between `api/cars` and `api/cars?color=red` calls with [FromQuery] object

I am using [FromQuery] atribute in controller's Get method:
//CarsController, etc..
[HttpGet]
public async Task<ActionResult<IEnumerable<CarsDto>>> Get([FromQuery] CarsParameter? carsParam = null)
{
//param is always not null here
}
Inside the method I need to distinguish between api/cars and api/cars?color=red calls. Problem is, that carsParam object is never null, so I cannot say if the Color="" (defailt value) is intended to be empty string or it's because of the call was api/cars
CarsParameter is a simple class:
public class CarsParameter
{
public string Color {get; set;} = "";
//more params here
}
Yes, I can use different path, like api/cars/withParams?color=red, but i am looking for more subtle solution.
I need to distinguish between api/cars and api/cars?color=red calls. Problem is, that carsParam object is never null
Please note that default model binding starts by looking through the sources for the key carsParam.Color. If that isn't found, it looks for Color without a prefix, which cause the issue.
To achieve your requirement, you can try to specify prefix explicitly, like below.
public async Task<ActionResult<IEnumerable<CarsDto>>> Get([FromQuery][Bind(Prefix = "carsParam")] CarsParameter? carsParam = null)
{
Request to api/cars?color=red&carsParam.color=yellow&carsParam.brand=test and following is test result

RedirectToAction with model and List properties

I have a 2 views with model as Account. From view one, I am using RedirectToAction to go to view two and sending the model object as below:
[HttpPost]
public ActionResult Login(Account account)
{
//Some code here
return RedirectToAction("Index", "AccountDetail", account);
}
AccountDetail controller looks like this:
public ActionResult Index(Account account)
{
return View("ViewNameHere", account);
}
The model object contains a property like this:
public class Account
{
// Some code here
public List<Details> Details{
get;
set;
}
In the first controller, before making call to RedirectToAction there is one item in Details. However, in the Index method of second controller, there is nothing.
Can someone help pointing out the flaw here? Since I am beginner with MVC, cannot seem to figure it out.
You should not pass a complex object to a GET method. Apart from the ugly URL it would create, you could easily exceed the query string limit and throw an exception.
In any case you cannot pass a collection (or a complex object containing a collection) to a GET method using RedirectToAction(). Internally the method uses reflection to generate the query string by calling the .ToString() method of each property of the model, which in the case of your collection property will be something like ../AccountDetail/Index?Details=System.Collections.Generic.List<Details>.
When the Index() method is called, a new instance of Account is initialized and the value of its property Details is attempted to be set to the string System.Collections.Generic.List<Details> which fails and the result is that property Details is null.
Options include passing an identifier and get the collection from the repository or Session or TempData

Provide common data to all ASP.NET MVC 4 controller method

I am rewriting an ASP.NET webforms app in MVC4 and was wondering how to solve the following problem. It is a multi-tenant app, so part of the URL has the tenant NAME in it:
http://mysite/tenant/controller/action
But tenant is an abbreviation representing the tenant, but I'd like to always convert that to the corresponding integer id and use that throughout the code. What is the best way to write that convert code once and have some variable/property available to all controller methods.
public class DivisionController : Controller
{
//
// GET: /Division/
public ActionResult Index()
{
// I want this.TenantId to be available in all controller methods
FetchDivisions(this.TenantId);
return View();
}
Is a base controller the best way to handle this or filters or attributes?
Yes a base controller will handle this just fine. If you need to perform a database lookup to convert the abbreviation to the integer value you can use the OnActionExecuting event like so:
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
// Lookup code here.
}

fluent nhibernate polymorphism. how to check for type of class

I have an Icon which has a Content (one to one) relationship.
public class Icon
{
public virtual Content Content {get; set;}
}
By default, it is lazy loaded which is what I want.
However, at some point in the code, I need to check what kind of Content is, Content being polymorphic, something like
if(icon.Content is TextContent)
{
...
}
Icon is part of another association and it is automatically obtained by the NHibernate, I do not get it manually.
What is the recommended way of checking for the actual type in this situation?
I can have a specific property like ContentType which will be an enum in order to identify the actual content type, but I am looking to know if there's a different way.
If you want to do that kind of check, you have to remove the proxy from the property.
There is a few ways to do it:
If you have access to the session call:
session.PersistenceContext.Unproxy(icon.Content);
Implement a virtual method (in a base class if possible) that forces the removal of the proxy by returning the instance with the proper type.
public virtual U As<U>() where U : YourType {
return this as U;
}
Disable the lazy initialization of the property.
This is very similar to another recent question.
To add to csanchez's list, a fourth method is to add a Self property to the Content base class that returns the un-proxied type:
public virtual void Self
{
get { return this; }
}
And a fifth method is to use 'lazy="no-proxy"` in the mapping as described on Ayende's blog.
Thanks for the suggestions but meanwhile I found an interesting solution, better I think.
Using the Visitor pattern, I can define an IconContent visitor and pass an Action to be executed to it.
For example, suppose there is a TextContent and an ImageContent, it will be something like this:
IconContentVisitor.Func(()=> { Console.WriteLine("this is TextContent"; }, ()=> { Console.WriteLine("this is ImageContent"));
Idea came from here: http://mookid.dk/oncode/archives/991