ASP.Net MVC Razor helpers in portable areas - asp.net

I have an ASP.Net MVC Portable Area project containing pure C# code, Razor views (cshtml), javascript and CSS. It's all being embedded into the output DLL for the area project.
I want to create a #helper function that can be accessed across the cshtml views in the area, and I followed Scott Gu's blog post to set this up. According to the Visual Studio intellisense and the compiler, everything was OK when I tried to access the helper like this:
#Helpers.MyMethod("myInput");
But when I tried running the application I got a runtime error stating that
"The name 'Helpers' does not exist in the current context"
I have put my Helpers.cshtml file in an App_Code folder in the root of my MVC portable area project, and set the build action to "Embedded resource" (as I need to do with the rest of my cshtml files).
I tested putting the Helpers.cshtml file in the App_Code folder of my ASP.Net MVC application project instead, and then it worked just fine, but it is not an option to have the helper in the application project, because I do not want dependencies from the area to the application.
It also works to rewrite the #helper to a #function and put it inside each of the CSHTML views, but I would like to avoid having this duplicated for each view.
So my question is why the application cannot seem to find the Helpers class when placed in the portable area project, and what I can do to be able to have razor helper functions inside my area that can be used across razor views.

Related

Create Controller and add Views to another project

Visual Studio 2015 + all updates.
Asp .Net Web application (MVC).
I start off by adding a few class libraries and separating the Asp .Net WA into layers i.e. DataAccess, Business Logic and the web project itself.
Once separated I add relevant references and everything is working as I expect it to be (i.e. the application functions as it did before I separated it into layers).
In my BL (Controllers are found here). I don't have the option to Add a Controller, like you would when right clicking the Controllers folder in the default project, so add the below line
<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
to the csproj file for my class library and the Add Controller option now appears. Create a controller but throws an error which was due to not having a web.config file - add this file and all works (although it would be nice to have this library working without a web.config file).
The problem I've hit is, when the Controller is created it also adds a View template within the class library but my Views folder is located in my web project.
Have I done this wrong? Is there a workaround so when a controller is created, it also creates the Views into the correct project? Or another approach for this?
This is just a guess, but it seems like you are try to use a UI-based architectural pattern to build your business layer.
Typically, your models, views, and controllers are all maintained in the main web-app project. Any supporting functions (like your BL and DL) are added via class libraries. The Visual Studio MVC templates are built around that concept, which is why you had to manually add support with the GUID - and why it automatically creates the view.
If I may ask, why are you trying to build controllers into your BL? If you are trying to decouple your UI from your server code, perhaps WebAPI would be a better option.
UPDATE - A few helpful links
ProDinner - ASP.NET MVC Sample App
N Layered App with Entity Framework, Autofac, ASP.NET MVC and Unit Testing
Architecture Guide: ASP.NET MVC Framework + N-tier + Entity Framework and Many More
Most of your issues boil down to using the scaffold. The scaffold is great when you're just starting out or for extremely simple projects, but it quickly falls down beyond that. Specifically, adding a controller via scaffold is designed for an MVC project, so it expects to find things you'd find in an MVC project. Additionally, it creates scaffolded views in an appropriate directory in Views because, again, that's what it's designed to do.
The simplest solution, then, is to just not use the scaffolds. A controller is just a class that inherits from Controller. Nothing special there. Then, you can create the views where you want to create them.

Reusable ASP.NET MVC components in dll like ASP.NET WebControls

I have several websites and these website share several components. It was quite easy with classic ASP.NET WebControls ascx. I created several such controls, put into one dll library and I reference these libraries from these websites via namespace.elements runat=server...
But I don't know how to do it after I have upgraded to ASP.NET MVC. I can put model and cotroller class into dll.
But how should I put and reuse Views into dll?
I suppose that Views are not compiled into dll, if I can change the View without recompiling the dll.
EDITS:
I would prefer some standard solution over third party. The last solution for me is to use StringBuilder instead of ViewEngine.
I've been using Razor Generator for several years to store reusable MVC views and helpers in separate .dll.
Razor Generator "is a Custom Tool for Visual Studio that allows processing Razor files at design time instead of runtime, allowing them to be built into an assembly for simpler reuse and distribution."
Installation instructions
It’s on the VS extension gallery, so install
it from there. It’s called “Razor Generator” (not to be confused with
“Razor Single File Generator for MVC”).
It is quite simple to use:
Usage in an MVC app
Install the 'RazorGenerator.Mvc' package, which registers a special
view engine
Go to an MVC Razor view's property and set the Custom tool to RazorGenerator
Optionally specify a value for Custom Tool Namespace to specify a namespace for the generated file. The project namespace is used by
default.
Optionally specify one of the generators in the first line of your Razor file. A generator declaration line looks like this: #*
Generator: MvcHelper *# . If you don't specify this, a generator is
picked based on convention (e.g. files under Views are treated as
MvcViews)
You'll see a generated .cs file under the .cshtml file, which will be used at runtime instead of the .cshtml file
You can also go to the nuget Package Manager Console and run 'Enable-RazorGenerator' to enable the Custom Tool on all the views.
And to cause all the views to be regenerated, go to the nuget Package Manager Console and run 'Redo-RazorGenerator'. This is
useful when you update the generator package and it needs to
generate different code.
MVC project should be chosen for class library in order to support intellisense and other useful features.
Usage in a View Library
If you need to create a separate library for your precompiled MVC
views, the best approach is to actually create an MVC project for
that library, instead of a library project. You'll never actually run
it as an Mvc app, but the fact that it comes with the right set of
config files allows intellisense and other things to work a lot
better than in a library project.
You can then add a reference to that 'MVC View project' from your real
MVC app.
And note that you need to install the 'RazorGenerator.Mvc' package
into the library, not the main MVC app.
Programming ASP.NET MVC 4 written by Jess Chadwick tells that
In the ASP.NET Web Forms world, you can achieve this by creating user
controls or custom controls that can be compiled into standalone
assemblies. These assemblies can be distributed across projects,
thereby enabling their reuse across projects.
The Web Forms view
engine offers the ViewUserControl class, which can be leveraged to
create such components for the MVC framework. The Razor view engine in
ASP.NET MVC, however, does not offer any such method out of the box.
and suggests using Razor Single File Generator visual studio extension, another one but the similar to Razor Generator approach.

Generate partial class for ASP.NET Web Forms when importing into a project

I've created a new ASP.NET Web Site in VS2008. I'm attempting to rebuild an old VS.NET application (.NET 1.0). I imported all of the WebForms from the old project (Add Existing items) and I ended up with a ton of errors like:
"The name 'lblMessage' does not exist in the current context"
The partial classes in this project don't have the object definitions for each of the UI elements. I'm hoping there's a way to generate these without having to try to hand-code 500 object definitions.
I ultimately found a workaround for this issue. By manually creating the designer pages ('mypage.aspx.designer.cs') file and then performing an edit on the markup page ('mypage.aspx') Visual Studio will automatically rebuild all the references.
Try to use ConvertToWebApplication in Context Menu , WebApplication project file.
This will Generated aspx.designer.cs with declaration for controls.

asp.net mvc localization. Views are localized, embedded resources in external DLL not

I have encountered a very strange issue with localization in ASP.NET MVC. Let me outline my projects since it is not that straight forward.
I have a TRUNK version of my website. The website uses two localization techniques. The first one is to load resources within the views from App_LocalResources. The second technique is to load resources from embedded resources in DLLs (project references).
On one view I show some lables which are localized from the view and I also load some labels via AJAX/JSON. The Action gets the localized data from an embedded resource in a referenced class project (DLL). Actually, in TRUNK, this works pretty well.
Now the Problem: I have a BRANCH which does not have a project reference to the embedded resource but a reference to a compiled DLL (copy). In the BRANCH the localization only works for the View, not for the AJAX/JSON call which simply returns the default locale.
What can be wrong? Do you have to make an settings on the DLLs if you redistribute them?!

ASP.NET v1.1 Could not load type TBRWEB.frmLogin

I have a web app that I wanted copy to a new project.
I created the new app with VS2003 and copied across the web forms, config, global asa, etc. and made sure IIS says it is a web app.
However, I still get this error when I run the app using IE from VS IDE:
ASP.NET v1.1 Could not load type TBRWEB.frmLogin
I also made sure the assembly name and root namespace are the same as original.
Any ideas?
These error normally occurs if the .aspx page or the Global.asax page contains a reference to a code-behind module and if the application has not been built.
Look in your project settings and check the value of the default namespace (in the application section).
Then check the "inherits" attribute in the control header of your ascx control (and yll other aspx/ascx files). I suspect that these two names are not the same. Creating a new project maybe you choose a different name which became the default namespace.
The simplest solution would be to change the namespace of your project to TBRWEB, if that is an option.
if it is not that easy, then my follow-up questions are:
Did you successfully build (compile) the application?
If it's a ASP.NET web site (not web project), did you forget to copy the .cs/.vb files in the App_Code folder?

Resources