Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 543 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ASP.NET MVC: No parameterless constructor defined for this object

#1
Server Error in '/' Application.
--------------------------------------------------------------------------------

No parameterless constructor defined for this object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.

Source Error:


Line 16: HttpContext.Current.RewritePath(Request.ApplicationPath, false);
Line 17: IHttpHandler httpHandler = new MvcHttpHandler();
Line 18: httpHandler.ProcessRequest(HttpContext.Current);
Line 19: HttpContext.Current.RewritePath(originalPath, false);
Line 20: }

I was following Steven Sanderson's '[Pro ASP.NET MVC Framework][1]' book. On page 132, in accordance with the author's recommendation, I downloaded the ASP.NET MVC Futures assembly, and added it to my MVC project. [Note: This could be a red herring.]

After this, I could no longer load my project. The above error stopped me cold.

My question is **not**, "Could you help me fix my code?"

Instead, I'd like to know more generally:

- How should I troubleshoot this issue?
- What should I be looking for?
- What might the root cause be?

It seems like I should understand routing and controllers at a deeper level than I do now.

[1]:

[To see links please register here]

Reply

#2
You need the action that corresponds to the controller to not have a parameter.

Looks like for the controller / action combination you have:

public ActionResult Action(int parameter)
{

}

but you need

public ActionResult Action()
{

}

Also, check out Phil Haack's [Route Debugger][1] to troubleshoot routes.


[1]:

[To see links please register here]

Reply

#3
By default, MVC Controllers require a default constructor with no parameters. The simplest would be to make a default constructor that calls the one with parameters:

public MyController() : this(new Helper()) {
}

public MyController(IHelper helper) {
this.helper = helper;
}

However, you can override this functionality by rolling your own `ControllerFactory`. This way you can tell MVC that when you are creating a `MyController` give it an instance of `Helper`.

This allows you to use Dependency Injection frameworks with MVC, and really decouple everything. A good example of this is over at the [StructureMap website][1]. The whole quickstart is good, and he gets specific to MVC towards the bottom at "Auto Wiring".


[1]:

[To see links please register here]

Reply

#4
I got the same error when:

Using a custom ModelView, both Actions (GET and POST) were passing the ModelView that contained two objects:

public ActionResult Add(int? categoryID)
{
...
ProductViewModel productViewModel = new ProductViewModel(
product,
rootCategories
);
return View(productViewModel);
}

And the POST also accepting the same model view:

[HttpPost]
[ValidateInput(false)]
public ActionResult Add(ProductModelView productModelView)
{...}

Problem was the View received the ModelView (needed both product and list of categories info), but after submitted was returning only the Product object, but as the POST Add expected a ProductModelView it passed a NULL but then the ProductModelView only constructor needed two parameters(Product, RootCategories), then it tried to find another constructor with no parameters for this NULL case then fails with "no parameterles..."

So, fixing the POST Add as follows correct the problem:

[HttpPost]
[ValidateInput(false)]
public ActionResult Add(Product product)
{...}

Hope this can help somebody (I spent almost half day to find this out!).
Reply

#5
The same for me.
My problem appeared because i forgot that my **base model class already has property with the name which was defined in the view**.

public class CTX : DbContext { // context with domain models
public DbSet<Products> Products { get; set; } // "Products" is the source property
public CTX() : base("Entities") {}
}

public class BaseModel : CTX { ... }
public class ProductModel : BaseModel { ... }
public class OrderIndexModel : OrderModel { ... }

... and controller processing model :

[HttpPost]
[ValidateInput(false)]
public ActionResult Index(OrderIndexModel order) { ... }

Nothing special, right? But then i define the view ...

<div class="dataItem">
<%=Html.Label("Products")%>
<%=Html.Hidden("Products", Model.index)%> // I FORGOT THAT I ALREADY HAVE PROPERTY CALLED "Products"
<%=Html.DropDownList("ProductList", Model.products)%>
<%=Html.ActionLink("Delete", "D")%>
</div>

... which causes "Parameterless constructor" error on POST request.

Hope that helps.
Reply

#6
First video on [

[To see links please register here]

][1]

47:10 minutes in show the error and shows how to override the default ControllerFactory.
I.e. to create structure map controller factory.

Basically, you are probably trying to implement dependency injection??

The problem is that is the interface dependency.


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through