Question

I wanted to try and use area (just trying out). I added a area foo to my project: right click on the project then add area. That folder contains sub folders where I can add controllers, view, models etc. It also has a cs file fooAreaRegistration.cs where routing for the area is done.

using System.Web.Mvc;
namespace AreasExample.Areas.foo
{
    public class fooAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "foo";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context )
        {
            context.MapRoute(
                "foo_default",
                "foo/{controller}/{action}/{id}",
                new {controller = "Foo", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}

Global.asax already has a function for registering area in app start

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    RegisterRoutes(RouteTable.Routes);
}
    }
}

I also then created a controller Foo which already has a default Index action, after that I added a view to the action. Based on the context.MapRoute in fooAreaRegistration.cs, if I run the program and go to this link http://localhost:54421/foo/Foo shouldn't it be working? It shows some error when I go to my area foo and controller Foo. Error says

    Server Error in '/' Application.

    [A]System.Web.WebPages.Razor.Configuration.HostSection cannot be cast to 
[B]System.Web.WebPages.Razor.Configuration.HostSection. Type A originates from 'System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages.Razor\v4.0_1.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'. Type B originates from 'System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages.Razor\v4.0_2.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'.

Is there something I'm missing? Do I need to add something?

Edit: I'm not sure if I should delete this post as I found the answer which is mentioned as answer below. But then it may be helpful for those who are following the same book(ASP.NET MVC4 in action).

Suggestions?

Était-ce utile?

La solution

That was probably because I downloaded the project of the book from internet which had older version of razor, and after I created an area in that project it couldn't cast it to the latest version of razor(i'm guessing) as the warning as in visual studio says:

  Warning   1   D:\Tutorial\mvc4ia-2012-06-


        13\src4\Chapter13\AreasExample\Areas\foo\Views\Foo\Index.cshtml: ASP.NET runtime error: 
        [A]System.Web.WebPages.Razor.Configuration.HostSection cannot be cast to 

        [B]System.Web.WebPages.Razor.Configuration.HostSection. Type A originates from 
        'System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, 
        PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 
        'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages.Razor\v4.0_1.0.0.0__31bf385
        6ad364e35\System.Web.WebPages.Razor.dll'. 
Type B originates from 'System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages.Razor\v4.0_2.0.0.0__31bf385
    6ad364e35\System.Web.WebPages.Razor.dll'.   D:\Tutorial\mvc4ia-2012-06-
    13\src4\Chapter13\AreasExample\Areas\foo\Views\Foo\Index.cshtml AreasExample
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top