ASP.NET MVC Web structure - asp.net

Database http://img443.imageshack.us/img443/6049/structure.gif
This is database structure. It is used to build simple cms in asp.net mvc. Before I run deeper into developing site, I want to make sure if this would be "correct" web structure:
Articles:
Controllers folder: Create controllers ArticleCategoryController and ArticleController
Models folder: ArticleCategoryRepository and ArticleRepository
View folder: ArticleCategory folder (Create.aspx, Edit.aspx, Index.aspx, Details.aspx); Article folder (Create.aspx, Edit.aspx, Index.aspx, Details.aspx)
Photos:
Controllers folder: Create controllers PhotoAlbumController and PhotoController
Models folder: PhotoAlbumRepository and PhotoRepository
View folder: PhotoAlbum folder (Create.aspx, Edit.aspx, Index.aspx, Details.aspx); Photo folder (Create.aspx, Edit.aspx, Index.aspx, Details.aspx)
and so on...
Is there a better way or this is ok?
Thanks.
Ile

There are lots of ways to organize your controllers and views. I usually create a controller for each full view.
The main comment I have here is about the model. I do not like putting model code into the models folder of the mvc web project unless they are view models or somehow directly related to the web tier of my app and inapplicable to a non web context. In my opinion, you are better off putting things like domain classes, repositories services, etc in a separate assembly or group of assemblies. This way you can swap out the front end with silverlight, or some other technology and continue to leverage your core domain code. Not only is this beneficial for swapping out front ends which is sometimes rare, but you can use the core domain dll for batch files or command line utilities if you ever need to do batch type operations outside of the context of your website.

Related

Silverstripe v4 MVC Directories?

I'm Wondering Why There is no "Models / Views / Controllers" Directories Inside SS4 Main Directory? It's Seems Like Everything Should Go Inside "mysite/code" .. How To Accomplish MVC Style?
MVC is more of a software-architectural pattern, not how you organize files. There are only a few assumptions the SilverStripe framework makes on how you should organize your code:
Model
With SilverStripe 4, the default code folder for every module is named src or code. In that folder you're free to organize your files as you see fit. Ideally you build your folder structure in a PSR-4 compliant way, so that your folder-names match with your namespaces.
Controllers
Same as with Model classes, you can put your controllers anywhere you want.
There's a default assumption for Page classes though. These expect that the matching controller has the same namespace as the page. So if your page is named: Company\Module\Pages\MyPage, the framework would look for Company\Module\Pages\MyPageController.
You're free to override that though, by implementing the getControllerName method on your Page and return the FQCN for the controller to use.
Views
Also known as "templates" should be in a templates folder. Each module (your mysite folder is also a module) can have a templates folder.
In addition to this, you can have themes. Themes usually bundle templates, css and other assets. In the themes folder you can have multiple theme folders, each one can have a templates folder.

How to reorganize contents like the old school in ASP.NET Core MVC?

I have a web site in ASP.NET Core MVC, and I don't want to put my contents inside another folder. I just want to use the old school of creating this file-system architecture at the root of my project's folder:
Styles
Fonts
Scripts
Images
But I can't make ASP.NET Core MVC serving files from them. For example http://domain.test/styles/default.css returns 404.
I've tried to add StaticFileOptions, but I can't get it to work.
Can you help please?
I tried the given solution, that is, to use UseWebRoot("") with empty string to make it refer to the root of my web project. Yet it still returns 404.
I created a Styles folder inside the \bin\Debug\netcoreapp2.0\ and added a Styles.css there and it served the file.
If you are not adding them on the wwwroot folder then you should do more preparation.
This link will help Working with static files in ASP.NET Core
I think before asking you should search some more. For example
this stackoverflow question will help you:
How to Configure an Alternative Folder to wwwroot in ASP.NET Core?

How do I get the ASP.NET content relative path

Doing a project between multiple people, and a few components (web app, services app and some others). We will be storing some information inside the Content folder of the web app so it can be accessed directly from the web server with an href, however other components outside of the web app need to access this folder as well, and since we are sharing the project between multiple people using an absolute path is not an option. What options do we have?
EDIT: Trying to explain it a little better.
What i have exactly is, a web project, a "data project" which is just a dll, a "logic" project which is another dll and a services project which is an exe/service.
Both the web project and service project consumes the methods from the logic, and the logic from the data project. Being the last one the responsable for storing data (in a database) and also in the file system.
This "filesystem" path should be configurable, and we are aiming to put it into the content folder of the web project so multimedia files can be accessed directly rather than doing a byte stream.
Now in the web.config(config file of the web app), and app.config(config file of the services app) i could set the absolute path to web/content (the same for both config files) and the data dll would use it without problems. Now the main problem is that we cannot put an absolute path in the config file because each person works on a different computer with obviously different file paths. So if i could just write something like: ~/project/Web/Content rather than C:/myfolder/stuff/blabla/project/web/content in the config files, with ~ resolving the path to the project, this is what i want! Or maybe better ideas about how to share a folder with these apps without adding absolute paths hardcoded somewhere.
What you want to use is:
Server.MapPath("/Content/filepath.ext");
This will give you the absolute path of a file based on it's position within the website, in this case, from the /Content directory.
For a program external to the website, you have a couple options;
The easiest to implement might be a simple configuration value in the external program which points to the directory. My guess is you've already decided that's not ideal, but it may be the quickest way.
Alternatively, there's a Microsoft .NET assembly which gives you easy access to IIS information (I can't recall its name off the top of my head!). You could use this assembly to find the appropriate website, and retrieve its root directory. I'll see if I can find it and get an example, or maybe someone else will see this and post an answer with that information.
Please check the following method "ResolveClientUrl"
MSDN
Use the ResolveClientUrl method to return a URL string suitable for use by the client to access resources on the Web server, such as image files, links to additional pages, and so on.
http://msdn.microsoft.com/en-us/library/system.web.ui.control.resolveclienturl.aspx

Asp.net mvc3 localization - linking resource with view?

I use the localization paradigm of one resx file per view in all my projects. I have two identical folder structures for Resources and Views and I link the resource and the view like so:
#using Res = [Namespace].Resources.[Controller].[View]
<p>#Res.[ResourceKey]</p>
Is it possible to get asp.net to use/link the resource file without the using statements and defining a Custom Tool Namespace for all my Resx files? (Yes, I am that lazy.)
Open your web.config in your Views folder.
Add the namespace for your resources.
Restart VS as it doesn't pick up the namespace so it's not showing intellisense.
Call your resources from any view without adding using statements inside your *.cshtml files.

two problems with Web Site type project (not Web Application)

I have a project which is a Web Site, not Web Application and have two problems because of that.
I have a custom control derived from BaseValidator which I had to put in the App_Code folder. Now on the page I want to register this control by
<%# Register tagname="mytagname" Namespace="PP" TagPrefix="dv2" %>
But when I get enter to this page, I have exception, because the src attribute is missing. I can't put in the src attr. path to the App_Code because I have another exception. How can I do that? This is a Web Site, so the sources are not compiled to one dll file, so I don't know what to add to the src attribute.
When I want to add to the Web Site a new project library, I have to compile it and manually copy the library dll file to the bin folder in the Web Site. But don't know, how can I debug this library with brakepoints?
This is my first time with Web Site project type. I always created the Web App.
1- Don't use tagname attribute here, tagname is suitable for UserControls.
http://msdn.microsoft.com/en-us/library/c76dd5k1.aspx
2- There is no any difference between using library in a web app or website. Just choose Add Reference from project node's context menu and select your class library from project tab.
Try creating a separate project for your CustomControls and reference it in your WebSite project by the namespace you use in your CutomControls project. Also check out this post for some information asp.net add custom control in website
Your second question, if you keep all of your projects in the same solution and just reference them to each other's projects, you will be able to debug and step into methods and set break points in any of the projects in your solution. If you don't want to do that, you have to copy the debug symbol files along with the .dll to the bin or point VS to where the necessary debug symbol files are located and then you will be able to step through the code in the .dll

Resources