Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin ASP mvc 4 action with parameters not working

I have these actions:

public class HomeController : Controller
    {
        public ActionResult Index ()
        {
            ViewData ["Message"] = "Welcome to ASP.NET MVC on Mono!";
            return View ();
        }


        public ActionResult Pages (string test)
        {
            ViewBag.Message = test;
            return View ();
        }


    }

The pages action is not working. I get an error 500:

System.TypeLoadException
Could not load type 'System.Web.UnvalidatedRequestValuesBase' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

Description: HTTP 500.Error processing request.

Details: Non-web exception. Exception origin (name of application or object): System.Web.Mvc.
Exception stack trace:
at System.Web.Mvc.FormValueProviderFactory.GetValueProvider (System.Web.Mvc.ControllerContext controllerContext) [0x00000] in <filename unknown>:0 at System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider (System.Web.Mvc.ControllerContext controllerContext) [0x00000] in <filename unknown>:0 at System.Web.Mvc.ControllerBase.get_ValueProvider () [0x00000] in <filename unknown>:0 at System.Web.Mvc.ControllerActionInvoker.GetParameterValue (System.Web.Mvc.ControllerContext controllerContext, System.Web.Mvc.ParameterDescriptor parameterDescriptor) [0x00000] in <filename unknown>:0 at System.Web.Mvc.ControllerActionInvoker.GetParameterValues (System.Web.Mvc.ControllerContext controllerContext, System.Web.Mvc.ActionDescriptor actionDescriptor) [0x00000] in <filename unknown>:0 at System.Web.Mvc.Async.AsyncControllerActionInvoker+<>c__DisplayClass21.<BeginInvokeAction>b__19 (System.AsyncCallback asyncCallback, System.Object asyncState) [0x00000] in <filename unknown>:0 

If I remove the parameter from that action is working fine.

Any ideas?

Thanks in advance!

p.s. I don't have any routes defined yet.

like image 749
Apos Spanos Avatar asked Feb 17 '14 10:02

Apos Spanos


People also ask

What will happen if you Cannot parse the parameter value of an action method?

Action Method Parameters If the parameter value cannot be parsed, and if the type of the parameter is a reference type or a nullable value type, null is passed as the parameter value. Otherwise, an exception is thrown.

What is action parameter MVC?

Action Method Parameters are most important in MVC. If you want to handle post request in action methods; MVC framework provided types of Action Methods Parameters. Action Method Parameters. We can organize the action methods for GET and POST requests separately.

What is mvc6?

MVC 6 is a part of ASP.NET 5 that has been designed for cloud-optimized applications. The runtime automatically picks the correct version of the library when our MVC application is deployed to the cloud. The Core CLR is also supposed to be tuned with a high resource-efficient optimization.

What is MVC action result?

An action result is what a controller action returns in response to a browser request. The ASP.NET MVC framework supports several types of action results including: ViewResult - Represents HTML and markup. EmptyResult - Represents no result. RedirectResult - Represents a redirection to a new URL.


3 Answers

Using newest mono(3.6.1) doesn't resolve issue. What helps is: If you're using 5.X NuGet Microsoft ASP.NET MVC package, downgrade to MVC 4 is required.

Steps:

  1. Uninstall following NuGet packages(order is important):
    • Microsoft.AspNet.WebPages
    • Microsoft.AspNet.Razor
    • Microsoft.AspNet.Mvc
  2. Install Microsoft ASP.NET MVC 4 NuGet package.

Errors that may occur:

  • The view 'Index' or its master was not found.
    • Solution - change version of System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc in /Views/Web.Config in factoryType attribute of host node to 4.0.0.0. So line looks like this:

<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

  • System.InvalidOperationException Conflicting versions of ASP.NET Web Pages detected: specified version is “3.0.0.0”, but the version in bin is “2.0.0.0”. To continue, remove files from the application’s bin directory or remove the version specification in web.config.
    • Solution - change webpages:Version to 2.0.0.0 in Root directory/Web.Config so it looks like this: <add key=”webpages:Version” value=”2.0.0.0” />

Then you can use parameters in controller actions.

like image 132
Daniel Kmak Avatar answered Sep 16 '22 11:09

Daniel Kmak


This is embarrassing, but here is my workaround until Mono updates System.Web:

public ActionResult Index() //string app_id, string session_id, int surah_id, int ayat_id)
{
    string app_id = Request["app_id"];
    string session_id = Request["session_id"];
    int surah_id = Int32.Parse(Request["surah_id"]);
    int ayat_id = Int32.Parse(Request["ayat_id"]);

    // ...
}
like image 31
mrwaim Avatar answered Sep 19 '22 11:09

mrwaim


I got the same error as OP in my MVC-project. (Currently using Xamarin on a Mac) I'm using Microsoft Exchange WebServices, and I found out that downgrading this package solved my problem. I downgraded the Microsoft.Exchange.Webservices package from 2.2 to 1.2.

like image 24
mathletics Avatar answered Sep 20 '22 11:09

mathletics