Lets Have a fun with Technology.

BTemplates.com

Follow by Email

Monday, January 25, 2016

AddMvc() method not found.


Hello,

Many developer do faces this problem during development.

Here some guideline to resolve this error.

1. On VS 2015, Tools >> Options >> Nuget Package Manager >> Package Sources, add https://www.myget.org/F/aspnetvnext/

2. Implemention should be as shown below.



using Microsoft.AspNet.Mvc;

using Microsoft.AspNet.Routing;

using Microsoft.AspNet.Security.DataProtection;

using Microsoft.Framework.ConfigurationModel;



namespace Microsoft.Framework.DependencyInjection

{

  public static class MvcServiceCollectionExtensions

  {

    public static IServiceCollection AddMvc(this IServiceCollection services, IConfiguration configuration = null)

    {

      ConfigureDefaultServices(services, configuration);

      services.TryAdd(MvcServices.GetDefaultServices(configuration));

      return services;

    }



    private static void ConfigureDefaultServices(IServiceCollection services, IConfiguration configuration)

    {

      services.AddOptions(configuration);

      services.AddDataProtection(configuration);

      services.AddRouting(configuration);

      services.AddContextAccessor(configuration);

      services.Configure<RouteOptions>(routeOptions =>

                          routeOptions.ConstraintMap

                            .Add("exists",

                               typeof(KnownRouteValueConstraint)));

    }

  }

}





For more this code refer below link
https://github.com/aspnet/Mvc/blob/27432d94530405f424e9015c8c5311907bd48b3f/src/Microsoft.AspNet.Mvc/MvcServiceCollectionExtensions.cs

3. If the executing request is a POST then it will try to find a method RandomSponsor acceptimg HttpPost. If make sense, you could remove HttpGet and that should do the trick.
This can also happen if you have many layers of calls that start with a POST (I had an action returning a view returning a partial view calling RenderAction), then the call to RenderAction will still look for a POST method.

Hope this will help you.

0 comments:

Post a Comment