.NET MVC Ambiguous Type Reference - asp.net-mvc

Not entirely sure what's going on here; any help would be appreciated.
I'm trying to create a new .NET MVC web app. I was pretty sure I had it set up correctly, but I'm getting the following error:
The type 'System.Web.Mvc.ViewPage' is ambiguous: it could come from assembly
'C:\MyProject\bin\System.Web.Mvc.DLL' or from assembly
'C:\MyProject\bin\MyProject.DLL'. Please specify the assembly explicitly in the type name.
The source error it reports is as follows:
Line 1: <%# Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
Line 2:
Line 3: <asp:Content ID="indexContent" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
Anything stand out that I'm doing completely wrong?

I suppose you named one of your page "ViewPage" is that the case?
And like #Jonathan mentioned, this smells:
Inherits="System.Web.Mvc.ViewPage"
On my MVC application, all the view pages have this instead:
Inherits="MySite.Views.Pages.Home"
Or something along the line. Your aspx page markup should have "Inherits" point to your code-behind class name, not the actual class that it is inheriting. The attribute name is rather misleading but its an artifact of earlier days.

Are you using a CodeBehind file, I don't see CodeBehind="" attribute where you are specifying the Inherits from? Then you have to point inherits to the class name of the codebehind.
Example:
<%# Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="MvcApplication4.Views.Home.Index" %>
Make sure the Inherits is fully qualified. It should be the namespace followed by the class name.

I'm running ASP.NET MVC Beta and also encountered this error.
It came about while I was trying to remove the code behind file for a view. I removed the "CodeBehind" attribute in the #Page directive and changed the "Inherits" attribute to point to System.Web.Mvc.ViewPage, but left the actual aspx.cs code behind file untouched in my project.
At this point, if I tried to run my project, I got the error you mentioned.
I resolved this error by deleting the aspx.cs (code behind) file for the view from my project.

Check your assembly references to System.Web.Mvc in web.config.
Mine were explicitly specifying 2.0.0.0, but my project referenced 3.0.0.0

This error usually indicates a class naming conflict. You are referencing two namespaces or you created a class with the same name in another namespace that you are using. I would start by looking at what that could be.

Inherits="System.Web.Mvc.ViewPage"
I'd imagine that this should be pointed at your View codebehind file class, not at the base ViewPage class.

Open up C:\MyProject\bin\MyProject.DLL in Reflector and look for ViewPage to see if you've defined one by accident.

No this should be a structural error message.
Check http://trikks.wordpress.com/2011/08/26/the-type-is-ambiguous-it-could-come-from-assembly-or-from-assembly-please-specify-the-assembly-explicitly-in-the-type-name/

I had the same exact error this week.
My Webform.aspx file had a generated designer.cs file. In this file the class was actually named "ViewPage". Delete the designer file, or move the class contents to the webform.aspx.cs file and my error was gone.
Just putting it out there in case someone had this error.

Related

Umbraco code behind doesn´t inherit type name

asax:
<%# Application CodeBehind="Global.asax.cs" Inherits="NestlePro.CustomGlobal" Language="C#" %>
And this class in code behind
Global.asax.cs content
The problem is that it doesn´t inherit the name or something else. Can you help me to find where is the problem? I´m not so skilled in .NET & Umbraco

Modifying T4 code templates for ASP.NET MVC to get rid of full namespace reference

We're using (there is need to tell where the files are, thanks) custom T4 code templates on creating a view or controller. Default implementation makes this kind first row.
<%# Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ProjectNameHere.ViewModels.ViewModelClass>" %>
We have already ViewModel and MVC namespace defined in the Web.config, so I would like code template to generate this.
<%# Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="ViewPage<ViewModelClass>" %>
Any suggestions how modify the default templates to get that kind of results? Which of the template lines actually generate these?
More information
I know where the files are and modifications has been made. Problem is that in the template they're using this
string mvcViewDataTypeGenericString = (!String.IsNullOrEmpty(mvcHost.ViewDataTypeName)) ? "<" + mvcHost.ViewDataTypeName + ">" : String.Empty;
It seems that ViewDataTypeName contains full namespace reference. I would like get just name of the class (in this case ViewModel class name)
You can find the templates here:
*\Microsoft Visual Studio \Common7\IDE\ItemTemplates\CSharp\Web\MVC 2\CodeTemplates*
There you can edit the generated code for each template, the template lines vary for each template so i cannot tell you, but you will spot them immidiately.
EDIT
The source for Web.Extensions is not available, but you can make use of relector and reflect on
\Microsoft Visual Studio \Common7\IDE\Microsoft.VisualStudio.Web.Extensions.dll assembly to see what "mvcHost" gives you :)
You will then notice a ViewDataType property which is a "Type", and you should then be able to create some logic around it to get the class name.

can you use Url.Content() in a master page

i keep getting Compilation errors when i try to do this . .is there anyway to do this inside the site.master file?
Did you add the master page as an MVC master page?
The Url property that you'rew trying to use comes from the MVC framework's ViewMasterPage class. If you added a regular master page, you need to make it inherit from ViewMasterPage. To do this, change the first line to
<%# Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>

Parse Error ViewMasterPage<TModel>

I am trying to implement a strongly typed master page and using this page as an example:
How to create a strongly typed master page using a base controller in ASP.NET MVC
I have the following declaration in my masterpage:
<%# Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<InsTech.ForeSight.Web.Mvc.ModelBase>" %>
When I try to run I get:
Parser Error Message: Could not load type 'System.Web.Mvc.ViewMasterPage<InsTech.ForeSight.Web.Mvc.ModelBase>'
I am not really sure why it can't find the type. When I use the ViewMasterPage class everything is okay, but when I try to use the generic version it bombs.
Any Suggestions
Is InsTech.ForeSight.Web.Mvc.ModelBase in another assembly? if so, is it referenced?
Is InsTech.ForeSight.Web.Mvc.ModelBase abstract?
I got this error when my MVC Master page was outside the 'Views' folder (with my normal Web forms master pages), moving it there solved this error for me.
Edit: Sorry I didn't see #Whoiskb already posted the same answer in the comments up there. Just leaving this answer for people who don't read all the comments :)

Remove the *.cs, *.Designer.cs codebehind files?

Yeah, its a bit on this side of pointless, but I was wondering... I've got all these codebehind files cluttering my MVC app. The only reason why I need these files, as far as I can tell, is to tell ASP.NET that my page extends from ViewPage rather than Page.
I've tried a couple different Page directives changes, but nothing I've found will allow me to identify the base class for the page AND let me delete the codebehind files.
Is there a way to do it?
UPDATE: I'm trying to inherit from a strongly-typed ViewPage! Seems like its possible to inherit from a regular ViewPage...
Delete the codebehind and use a page directive like this:
<%# Page Title="Title" Inherits="System.Web.Mvc.ViewPage" Language="C#" MasterPageFile="~/Views/Layouts/Site.Master" %>
Or, if you want to get rid of the codebehind but still want to use strongly typed view, then read this link: http://devlicio.us/blogs/tim_barcz/archive/2008/08/13/strongly-typed-viewdata-without-a-codebehind.aspx
Here is a cut and paste of what this would look like:
<%# Page Inherits="System.Web.Mvc.ViewPage`1[[ABCCompany.MVC.Web.Models.LoginData, ABCCompany.MVC.Web]]" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" %>
Assuming you don't have any code in your codebehind, why don't you point them all to one codebehind file?
Straight out of the box you should be able to delete the .designer.cs and nothing will break. The other code behind can be useful, for instance if you'd like to strongly type your viewdata.

Resources