ASP.NET Mvc convert <%# Import Namespace="Thirpartynamespace" %> to Razor View - asp.net-mvc-3

I'm trying to convert the mvc 3 aspx view engine to razor view engine below:
<%# Import Namespace="ThirdpartyComp.Web.UI" %>
<% = Html.SomeFunction("Test")%>
to:
#using ThirdpartyComp.Web.UI
#Html.SomeFunction("Test")
When I do #html.SomeFunction(... I get this exeption:
Are you missing a using directive or an assembly reference?
Could anyone please confirm if above is possible, and anything that I'm missing?
Thank you heaps.

The two code snippets are precisely equivalent. The problem lies somewhere else. Ensure that you have referenced the assembly containing this helper to the new project (Add Reference... in the project) and that this assembly is compatible with ASP.NET MVC 3 i.e. it depends on the System.Web.Mvc V3 instead of V2. If this is not the case you might need to recompile it against ASP.NET MVC 3 (if you have the source code) or ask the author for a version which is compatible with it.

Related

ASP.NET Core MVC controllers in separate assembly

I'm using ASP.NET MVC Core RC-2. I have a web project targeting the full .NET framework. I also have a separate class library in the solution, also targeting the full framework.
In the class library, I have a controller, marked with a route attribute. I have referenced the class library from the web project. This assembly references the nuget package Microsoft.AspNetCore.Mvc v. 1.0.0-rc2-final.
It was my understanding that this external controller would be discovered automatically, e.g.
http://www.strathweb.com/2015/04/asp-net-mvc-6-discovers-controllers/
However this doesn't work for me- I browse to the URL of the route and I get a blank page and it doesn't hit my controller breakpoint.
Any ideas how to get this working?
Interestingly, it does seem to work for web projects targeting .NET Core Framework, referencing a class library also targeting .NET Core. But not for a web project targeting the full framework, referencing a standard .NET class library.
Note: this is MVC Core which is supposed to support this kind of scenario without any MVC<=4 routing overrides.
Still an issue in ASP.Net Core 1.0, not sure if it's by design now. Easiest solution is to do this in Startup.cs/ConfigureServices
services.AddMvc()
.AddApplicationPart(typeof(<class in external assembly>).Assembly)
.AddControllersAsServices();
AddApplicationPart explicitly includes the assembly in searches for controllers.
The call to AddControllersAsServices() will add all the discovered controllers into the services collection, and if you put a breakpoint after this line and inspect 'services', you will see in the collection all the controller types which have been found.
You might also want to check here: https://docs.asp.net/en/latest/migration/rc1-to-rtm.html#asp-net-5-mvc-compile-views as the discovery rules are now changed for controllers from RC1.
Also remember to use IActionResult instead of ActionResult!
I believe you are hitting the following known issue in RC2.
https://github.com/aspnet/Mvc/issues/4674 (workaround is mentioned in the bug)
This has been fixed since then but will only be available in next release (unless you are ok with using nightly builds)

Using Assembly Reference in Global.asax?

I have an app that our offshore developers are trying to update. The code built with no issues a year ago. Now when they checkout the code and try to build it they get a reference error in Global.asax:
Error 1 The type or namespace name 'HartSourceLib' could not be found (are you missing a using directive or an assembly reference?) c:\Users\rs02130\Desktop\bip\BIProductionSupport\Global.asax 24 9 BIProductionSupport
There is a reference in the Project References pointing to the HartSourceLib DLL. The DLL is present in the referenced location and is the same DLL used in the original build. I'm not familiar with the use of external references in Global.asax. We've tried Using, <% Assembly %> and <% Import NameSpace %> and none have resolved the problem. Here's a sample of Global.asax:
Are we missing something in Global.asax? If not what could be causing the reference issue?
Notes: They're using VS2010 and the 3.5 Framework. I converted the Solution to VS2012 and the 4.5 Framework. Neither works.
Make sure the namespace is correct.
then Add
<%# Import Namespace="HartSourceLib" %> at the start of the code below the
<%# Application Language="C#" %>
After that, make sure HartSourceLib exist in web.config file, then mark HartSourceLib file to be copied to output folder on the file properties.
Finally clean your project and build.
I hope that solve your issue.

MVC-3 Doesn't Find Pre-compiled Views

I have a common MVC3 project which has some views which are compiled into the assembly using RazorGenerator. I've confirmed that the classes show up in the resulting DLL. I've referenced this project from another MVC3 project. When I try to use a view in the common MVC project (the view's namespace is CommonMvcProject.Views.Shared, for instance)...
#{Html.RenderPartial("ViewFromCommonMVCProject");}
...I get an error:
The partial view 'ViewFromCommonMVCProject' was not found or no view engine
supports the searched locations.
I don't want to use the physical views, I want to use the pre-compiled views in the common MVC project's assembly. How can I configure this project to search within the common assembly?
The following post looks like it has the details you need.
http://www.chrisvandesteeg.nl/2010/11/22/embedding-pre-compiled-razor-views-in-your-dll/
Hope that helps.

Combres, compress and combine .js and .css files

I'm trying to add the following to my ASP.Net solution. I installed it using the nuget package "Install-Package Combres.Mvc"
so far I'm not having the best luck with it as I keep getting the following error
'System.Web.Mvc.HtmlHelper' does not contain a definition for 'Combres' and no extension method 'Combres' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)
I also followed the advice of this link... and got same error:/
I'd like to make use of some compression & combination tool/framework so I'm hoping someone has any experience with this
I scrapped the whole combress/casette route after a few failed attempts. I read up a bit and saw that Asp.Net MVC 4 supports bundles/combining & compressing natievly. Which worked quite well for me.
Do you have
<%# Import Namespace="Combres" %>
On the top of the page to include the Combres namespace?
For more information (and the Razor syntax) you can also look at: http://combres.codeplex.com/wikipage?title=5-Minute%20Quick%20Start&referringTitle=Home

ASP.NET MVC 3.0 - Why strong-typing Model in a View, if static compilation is not happening?

I'm actively developing desktop applications, local and network services, some classic ASP.NET, etc., so I'm used to static compilation and static code analysis. Now that I'm (finally) learning ASP.NET MVC 3.0 I'm seeing that many of the ASP.NET MVC experts and experienced developers are recommending using strongly-typed views in ASP.NET MVC 3.0 (where applicable).
I'm guessing that "strongly-typed" means writing #model=... at the top of a view's code. But in doing that I only get IntelliSense to work, no static code checking is taking place. I can write anything I want in the #model statement in cshtml and it would compile and run. Consequentially, Model.Anything also compiles. In fact, if I don't type #model I can dynamically use whatever model I want that has "compatible" properties and methods.
I'm used to "strongly-typed" meaning "won't compile", like LINQ to whatever just will not compile if you don't get the properties right. Is there any other purpose for #model other than IntelliSense and a run-time error, and why is it called strong-typed if it's in fact, not?
Strong typing, Meanings in computer literature
Views are compiled at runtime by default. You can modify your project file (csproj) to compile the views when the application builds by setting the following property:
<MvcBuildViews>true</MvcBuildViews>
Downside of this approach is that your buildtime will increase significantly. You should consider only setting this option to true for release builds.
You can edit your project file by unloading the project, right-click the project and choose Edit ProjectFile
It is possible to setup your project so that it includes views in your compilation. This would be where static typing is useful. Another place would be in runtime, if you try to pass in a model that does not match the expected model you will immediately get an exception. If you were to type views dynamically then you would not know your model was invalid until your view tried to access a property of the model and finds out it isn't there.
The second scenario is also a nightmare if you pass in the wrong model object but it happens to have the same named property as the expected model. Then you just get invalid data and debugging becomes hell.
model is new dynamic type in .net 4.0, so these types get resolved at runtime not at compilation time.

Resources