Where to put settings of GUI/View on MVC? - c#

On my C# 2.0 (.NET CF) program, I need to create a theme based GUI (with Controls that are non .NET and 3rd party) . I will be doing it in such a way that the user can customize the colors, fonts, toolbar/tabcontrol positions and etc. Then I need to store this into some file/XML then later on retrieve it by just using a simple serialization/deserialization. But my problem is where to put this? shall this go to model, view, or controller?
thanks

A theme based user preferences information is "data" and does not belong to any of the main MVC folders. Data is always seperate from these folders.
If you are sure that you don't want to store it in database or NO-SQL data storage, but want to use a file instead, you can create a seperate folder in the root folder for this purpose. Alternatively you can use resx files and .NET ResourceManager class if your preferences will be key-value pairs. (RESX tutorial : http://msdn.microsoft.com/en-us/library/gg418542(v=vs.110).aspx )
Please see below for summaries of what model, view, contoller should contain and why they are not suitable to include theme based user preferences.
MODEL: The model "represents" the data, and does nothing else. Representation is the keyword here. User theme preferences are actual data and does not belong to here.
VIEW: The view displays the model data, and sends user actions (e.g. button clicks) to the controller thus it's not suitable for storing a user preferences data.
CONTROLLER: The controller provides model data to the view, and interprets user actions. Your file clearly is not a controller and
does not belong to here.
(For a detailed explanation of what MVC concepts are and to see some examples: http://blog.codinghorror.com/understanding-model-view-controller/)

Related

creating many instances of a webpage from one template?

I'm creating a new website application in asp.net. The landing page needs to have a button (or something similar) which the user can click to create a new instance of a webpage. Similar to how a Facebook user can create a new group/event or a StackOverflow user create a new question.
My website needs to be able to create multiple "events" from the landing page which can then be accessed from the landing page, each event should be a template populated with user details on creation.
Can someone please tell me how people refer to this technique of creating many instances of a webpage (event) from one template?
With ASP.NET Core MVC (using this as an example as you have an ASP.NET tag and your description doesn't specify a technology), you can create a template using a .cshtml file. If you are not familiar with these types of files (which are used within the ASP.NET framework), then I suggest a read of it here:
https://www.w3schools.com/asp/razor_syntax.asp
Roughly, it's a file with HTML content where you can easily embed .NET types (such as types from your Model) and .NET logic using "Razor syntax", so that your HTML file is modified appropriately (e.g. with queried data specific to your user) before being sent back to the client. The reference above gives good examples, so I'm not going to waste space and repeat them here.
You can have certain .cshtml files as your "template" and embed appropriate model data using Razor syntax. You can then have a hyperlink tag (for example) reference the .cshtml file using the asp-action attribute. This will render the .cshtml file to the client whenever that tag is clicked on. ASP.NET uses types called Controllers to handle such requests (Controllers are types that inherit from the Controller type) appropriately, such as querying the correct database and providing your .cshtml file with the correct data before sending the result back to the client.
ASP.NET Core MVC modularizes the types of actions described above very well (M --> Model, V --> View, C --> Controller). Here is a good reference:
https://learn.microsoft.com/en-us/aspnet/core/mvc/overview?view=aspnetcore-2.2
For other technologies that you wish to use to achieve the same result, you will have to consult the appropriate references.
Stackoverflow is a place to get answer on specific problem with short answer. Your problem is general design and programming question, and requires understanding of basic programming aproaches.
For that you should grab a book and read about designing webapplications in .Net.

MVC Page Template Configuration in the View file itself?

I want to render an MVC Page/View that will have varying widgets on the page. So some views might have Widget A, another page could have Widget A and B, etc. I was thinking each widget would be a partial view that I want to pass parameters. So if it was a weather widget, I would need to pass the partial view the Zip code etc.
My question is what the best way to approach this architecturally? We currently have an external xml file that stores this info: myview.xml (for myview.cshtm)
We don’t want to have an external file, but would like to store everything in one place maybe in the header of the view file itself? Any recommendations?
Independent widgets on your views?
Sounds like you just need to use RenderAction to render them.
More info by Haack
MSDN

Yet another .net custom cms

I'm trying to switch my custom cms written in php into .net c#. I was thinking to use cms as a learning project. I'm going to use C#, NHibernate ORM layer, mssql, mvc3 and jquery.
I'm aware there are plenty of commercials or open source cms, but still I'm going to spend some of my free time trying to learn new technology working on project like this.
So, is there anyone out there who is willing to share some ideas on creating cms domain model, usefull link, ideas, etc.
Thanks
A really basic CMS consist of 3 elements:
one database table to hold your "pages". The table structure is "name" and "content"
a route to transform requests of type /cms/pagename to a fixed controller, the method called cms and pagename as a parameter
a embeddable html editor
Now, there are two ways your "page" can be invoked. It is either create mode or view/edit mode.
In "create" mode, the page is requested but it is not in the database yet (e.g. cms/announcement1). In this mode you create a view consisting of a html editor and upon submit, you persist the page to the database.
In "view" mode, the page is requested and is IS in the database. You perform any necessary rewriting (for example you rewrite internal links of the form [cms/pagename] to a fully routable http address) and render the content.
If the user is authorized to edit the page, you also show a "edit" button which then invokes the html editor with the page loaded and ready to be edited.
And that's it.
There are tons of additional elements (caching, different built-in page types, embedding images, youtubes, preformatted texts) etc. but all of them are optional and you can introduce new features when you have the core already implemented.
Once I wrote a simple CMS following the structure above, it was a part of a bigger solution and till now it's been sucesfully deployed several hundred times. An advantage of a custom CMS is that it can be really simple and easily maintanable.

Architectural guidance for an eBay-like "Contact Us" system with a decision "workflow"

I am creating a new support center and "self-help" customer service module for an application. The CIO really likes the flow of eBay's "Contact Us" pages, that basically work like this:
First, you select a specific topic from a group of topics (e.g. Buying, Selling, Account on eBay)
You're then presented with what appears to be one of three variable types of information, based on the topic you picked (names are just what I'm calling them in some preliminary sketches):
"Descriptive": displays rich text with possible links to other parts of the application.
"Choice": Displays a list of additional topics
"Action": Lets the user look up an item and do some action (e.g. cancel)
From some experimentation, a choice can list to other choices, or to a descriptive block of text, or to an action section.
I'm turning up blanks as to the proper architecture for this. My platform of choice is ASP.NET (WebForms, sadly; we have no desire to touch MVC here) so the "Action" areas would have to be a user control that's dynamically loaded into a placeholder, but I'm more concerned with a possible database structure for this. I would need a way to know if each topic leads to one of the three types above and then on the page dynamically load either the content, list of links, or user control which makes things a bit trickier, nevermind the fact that a non-technical user will have to update and add the information from some kind of administrative panel.
Any suggestions for doing something like this? I'm not on a tight deadline, but I can't take too long or I'll be considered to be wasting time and not producing results.
If you can store the "tree of knowledge" in some way, like a custom XML file which would organize all options / possible actions, descriptions etc. Then you can "walk" it based on user's selections and display appropriate user control with content generated on the fly based on the contents of the XML node you're currently at.
Your "admin tool" would then need to update/modify the XML file, and your "public" CMS would render user controls inside an ASPX form.
One of the projects I worked on used this methodology for intranet's user menu - effectively a knowledge base of hyperlinks / actions split in to categories so they can be drilled-down to. Each element can contain links to other elements - so you have a spider-web like navigatable chain / workflow.
Just make sure each element has a unique ID (trivial to implement) and you can always get at it through xpath.
By having users modify a "working copy" and keeping backups of the live XML file when changes are published you also get versioning / roll-back which would be difficult to do in a DB.
If I personally was doing this I would just roll some MVC3 controllers that handle the work flow steps as needed. That seems to be out for you however.
With webforms, I would most likely consider handling this using Windows Workflow Foundation (the learning curve is moderately steep on this). Here's a pretty good example on using WF Flexible Web UI Workflow application through ASP.NET MVC & Windows Workflow Foundation. It's built on MVC however you could easily replace the return Views() with return UserControls.
Following a model like this would defacto give you the MVC pattern. The controller dictating flow matches very well for a workflow scenario.
Edit: Since this even seems out of the question, at this point you're best option is just writing a controller class that will manage the flow manually (probably a bunch of state / if checks) and then redirect users or return the appropriate user control.

Path To Files Inside Content Folder (ASP.NET MVC)

There something I still do not understand about how the Content folder works in ASP.NET MVC. To make things clearer I have a few questions:
Is the Content folder the root folder? I mean does http://localhost/ point to Content or is it something else?
I have a file named dummyIcon.png inside Content/images/temp folder. How do I locate it from my domain layer (which is a Code Library project)?
What is the best practice of displaying images in ASP.NET MVC? Should I store a path to the image in the database (which I personally prefer), or do I save a byte array and return it to the view?
I found the following links to be helpful within the context of the MVC web application, but I'd still appreciate some answers to the questions posted above. Thank you.
Can an ASP.NET MVC controller return an Image?
how to display image using view and controller by ASP.NET MVC
Anything in the root will point to the root if it is ignored by your routes:
If you have an image placed on the on the root of your project. Then, say http:://localhost/dummy.ico" will give you a 404, no controller found. Until you do this in your global.asax.cs:
routes.IgnoreRoute("dummy.ico");
//you could add wildcards here to match different things
From Code if you use says File.Open(); you need the physical path to the file. You get it like this:
string filePath = Server.MapPath(Url.Content("~/Content/Images/Image.jpg"));
It is upto you here, although I would say, putting files into the database makes a lot of sense if you want everything in one place. If you need to move your app around you would just move the data base.
When it comes to file paths, please remember you don't want duplicate file names, so you will have to give each file a GUID and then link it up. It could make sense if you have a large number of files (or large files itself) so you're database won't grow like crazy.
HTH
1.Is the Content folder the root folder?
I mean does http://localhost/ point to
Content or is it something else?
No, http://localhost:port/ does not point to content folder. You can access files in content folder through http://localhost:port/content/...
2.I have a file named dummyIcon.png
inside Content/images/temp folder. How
do I locate it from my domain layer
(which is a Code Library project)?
You should be able to access it as http://localhost:port/Content/images/temp/dummyIcon.png
3.What is the best practice of
displaying images in ASP.NET MVC?
Should I store a path to the image in
the database (which I personally
prefer), or do I save a byte array and
return it to the view?
Where you store the images depends on your application needs. Are these generic images that is used to display application images (icons, company logo, etc).. Then it is best to store them in file system.
If your application deals with images and you work on storing images, manipulation etc, then you may need DB. I think, storing images used on the web application is a overhead.
You should make a model object for your controller to return. in this example i am returning SearchPageModel, a class i have created. but lets say this object has a property called imageURL
but make sure the controller actually returns an ActionResult
so for example...
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Search()
{
SearchPageModel Model = new SearchPageModel();
// populate the Model properties
Model.ImageURL = "myjpeg"
return View("Search", Model);
}
i then pass this model object back to my desired view in this case my "Search" View
and to display the image, in the view i would add..
<img src="Images/<%=Model.ImageURL %>.jpg" />

Categories