I'm trying out ASP.NET 5 with MVC 6 but have some issues.
Using Visual Studio 2015 Comm
I create a model under namespace MyApp.wwwroot.Models.
Then a service under namespace MyApp.wwwroot.Service. Here I reference my model. It just shows in black, not in blue. It also has no quick actions or squigly line. When I build it says "Build Succeeded". I have the same problem when I add a reference to Microsoft.AspNet.Mvc in my Controller. When I create a new controller and inherit from Controller. I have the same issue..
Is this VS or what could be the issue?
It looks like you're trying to place code under the wwwroot folder. The wwwroot folder in ASP.Net Core (formerly ASP.Net 5) is used for hosting static content and these files are not parsed or compiled as C# code, but rather as static content that you want served (ex. html/css/javascript files). Moving these files out of the wwwroot folder should fix this issue if this is the case.
When working in your FishtankApp.Services namespace, you need to expose your model namespace. this is done by adding the following using declaration at the top of the source code document you wish to use it on:
using FishTankApp.Models ;
The same holds true for anything you wish to use that falls outside the scope of the current namespace you're working in.
Related
I have written a JavaScript which I would like to inject in swagger with .Net Core 2.2. I would like to highlight that wwwroot folder wasn't created by default when I created web project (empty project option) with .Net Core 2.2. I then created wwwroot folder manually and added my JavaScript file inside that. When I am launching swagger, I am getting an error that file couldn't be loaded. I tried to create a folder "swagger" inside wwwroot and place a file there but that too didn't work.
Here is my sample code:
SwaggerUIOptions uiConfig;
uiConfig.InjectJavascript("mycustomfile.js");
Also I would like to highlight what I am trying to achieve here. I got a drop-down in my header for a database provided i.e. Oracle/MSSQL etc. Now based on the selection, I want to change the value of another header i.e. connection string. I have written a JavaScript for this purpose but let me know if that can be achieved in some other and simplistic way as well with .Net Core along withSwashbuckle.
The issue got resolved after using app.UseStaticFiles(); in Configure() method.
I have an MVC 3 app that has some core functionality (most important is autorisation) but mainly serves as a portal to different areas or modules. I want to organize thit to different modules that with minor changes also can be deployed as their own website.
The project consists of a Forum, Blog engine, Messaging between users + 4-5 upcoming modules.
I looked at ScottGu's blog about MVC 2 and found something that seemed perfect:
Depending og what the customer need I want to only give them the exact modules they can use. It is also easier from a maintainence view to be able to work and update referencd assemblies in each project and just do a full update for the customers that have that spesific module on their server.
But in MVC 3 there is no apparent way to use Areas this way, do you know how?
Status
I will try to add MVCContrib Portable areas to my existing solution and convert my areas ower and will post back the results. If it works I will mark it as the accepted solution.
MVCContrib has portable areas.
http://mvccontrib.codeplex.com/wikipage?title=Creating%20a%20Portable%20Area&referringTitle=Documentation
This is possible in MVC3:
From:
http://bob.archer.net/content/aspnet-mvc3-areas-separate-projects
Right click on the shell project and "Add Area...". Type in the area name. This will create an Areas folder with your area in it. (This is not 100% needed but you do need the "Areas" folder and you can steal the XXXXAreaRegistration class for your application.)
Create a new MVC3 empty project in your solution to match your area. Move the XXXXAreaRegistration.cs file from the shell mvc project to the new project and adjust the namespace as applicable. (Or you can manually create an area registration class, it's a pretty simple class. Just use the Add area template generated one as an example.)
Edit the routes in the AreaRegistration folder as needed.
Delete the folder under the areas folder that the template wizard added.
Modify the web.config of the new project and take out the connection strings and the authentication, membership, profile, rolemanger sections. You will not need to deploy this web.config but the razor intellisense doesn't work without it during dev time.
Delete the global.asax file from the area's project or you will get extra default routes.
Create a virtual directory in the "Areas" folder of the shell project with the name of your area as the alias and point it to your "area" project. You will need to use IIS or IIS Express for this. I use IIS. For IIS Express you can use the appcmd.exe in the IIS Express folder or you can edit the applciationhost.config file.
I am currently stuck trying to add my own class file into ASP.NET MVC project, so it could
be referenced by my controller. But Visual studio 2010 always complain about "The type or name 'Products' could not be found (are your missing a using directive or assembly reference?)
I am not sure where to put my class file and tried add the class to Models or Controllers directory, and it wouldn't work either. My the class is under the right namespace, and I did reference the Models namespace in my Controller. It could be something obvious, but I couldn't get it working:(
Make sure that the "Build Action" property of the file is set to "Compile" in Visual Studio. Perhaps when you initially added the file, you added it to a location or added it as a file type that did not automatically set the build action properly.
The general rule of thumb is that business objects go it the Models folder you're correct. If it isn't working you don't have the project configured correctly. In the controller where you wanted to reference the Products object, did you add a reference to Models.Products?
Are there any other errors in listed? Sometimes multiple errors in a solution can combine to create something like this.
Ensure the namespace of your class is correct.
Based on a comment to a different answer, it appears you're trying to add a utility or helper class. I suggest you put it in a Helpers folder under the root of the project.
So first add the Helpers folder under the root of the project. You should have the following structure when done (there may be other folders as well):
[project]
- Content
- Helpers
- Controllers
- Views
Now, add a new class to the Helpers folder. Call this class ProductsHelper (in the Add New Item dialog make sure to put ProductsHelper.cs, as it's asking for the file name).
Now have a look in the new ProductsHelper.cs file. Copy the namespace found in said file, to be used in the controller.
Now, add a using directive at the top of your controller that looks like the following:
using [copied namespace];
Sometimes projects are set up to use default namespaces that don't match the project name, in which case the namespace in your newly-added class file might be different than what you thought you should add as the using directive. If you're wondering what the default namespace for your project is, you can see it in the project's properties, on the Application tab, in the "Default namespace" textbox (you can change it here as well).
I had the same problem adding some utility class .cs files to my new MVC project.
I was working mostly on VS 2010 Website projects which compile differently from
Web Application Projects ( WAP ) like the MVC 3 Project.
Just to clarify Jacob's answer, In the VS Solution Explorer: right click on the class .cs file and select the Property Dialog and then set the property: Build Action to Compile.
This will force the code to be compiled when you Build the project.
HTH, LA Guy
This code was generated for me after added entity framework code-first for SQL Server CE using NuGet. They did no changes to any other file. The file SQLCEEntityFramework.cs was created and placed in App_Start folder.
Does this mean it automatically gets executed or something? The same thing happened when I added Ninject for MVC 3. No code was added to the global.ascx file so I have no idea if its plug and play or I have to configure something.
[assembly: WebActivator.PreApplicationStartMethod(typeof(StackTorrents.WebUI.App_Start.SQLCEEntityFramework), "Start")]
According to:
http://haacked.com/archive/2010/05/16/three-hidden-extensibility-gems-in-asp-net-4.aspx
This new attribute allows you to have
code run way early in the ASP.NET
pipeline as an application starts up.
I mean way early, even before
Application_Start. This happens to
also be before code in your App_code
folder (assuming you have any code in
there) has been compiled. To use this
attribute, create a class library and
add this attribute as an assembly
level attribute. A common place to add
this would be in the AssemblyInfo.cs
class within the Properties folder.
To clarify, it gives you a way of hooking into several application start and application shutdown events WITHOUT having to change any existing code files (previously you had to edit Globals.asax.cs).
This is mostly a big deal when making packages as these events are really useful for bootstrapping Http modules and it is really difficult to write code into existing files.
Can we deploy an asp.net MVC application by copying the source codes only (without compiling them first) like we can do when deploying WebForm website project?
No - MVC apps need compilation, like ASP.NET Web Application Projects.
Wait... if what you mean is copying the *.aspx and *.ascx files, yes you can, but you have to include the *.dll in the bin folder too.
Copying the *.cs files is useless.
Start by building your application, then select the project (not the solution) that is your StartUp project (the one with the aspx files), then, in the main menu, do Build > Publish Selection and follow the wizard.
I'm afraid the answer that has been marked as correct is incorrect.
You can have your controllers inside your App_Code folder. You should follow the naming conventions for your views' folders (~\Views{ControllerName}{ViewName}.cshtml), and simply ensure that your route map correctly wires up the controller. If you do create your controller using the default App_Code behavior, it will not be placed inside a namespace. You should either wrap it in a default namespace ({SiteName}.Controllers) or explicitly set the namespaces: argument to include an empty string for the namespace.
Only if the code you are talking about reside in the aspx/ascx files. Most of your code should reside in your middle tier/controller/data tier - so having too much code in the views is usually considered as a bad practice in logic/concern separation.