How to create dynamic views in asp.net mvc? - asp.net

How to create dynamic views in asp.net mvc 2.0?

A better way to think about this is to have a generic view page that renders some HTML that administrator defines and which gets stored in a database. Another alternative would be to create static HTML files -- assuming the content is fixed or could be retrieved via AJAX -- and have a view which dynamically chooses which of these HTML files to from your generic view. I use this latter method to make certain parts of a link aggregator site I've put together maintainable by others. It renders HTML fragments (stored in content/static/*.htm) onto fixed parts of the index page/view using jQuery load. The admins have access to modify these files to rearrange/add/delete the links contained on that part of the page. The app is at an early stage of development and eventually these will migrate either into configuration files, and I will generate the HTML, or perhaps I'll develop an administrative interface to manage them and keep them in the DB.

Related

Add an asp code into another asp

In jsp and php it's very common to have files with pieces of HTML/Java/PHP code and have other jsp/php files consume them. That's fundamental for reusability.
In asp, M$ created another approach, master pages. But I'm having trouble to get used to it, to be forced to have ALL reusable code in a unique file. In example, some apps with many pages may have a breadcrumb area, while simpler apps won't need it.
Yes I can put those blocks inside a ContentPlaceHolder and just add them empty to asp pages they aren't needed. But I'd rather have them on their own files and include them when needed, instead of "removing" them when not needed.
Another example is about menus. Each app has its own menu. In jsp and PHP I can have a local file with the HTML menu and add it. In asp, the best I could imagine is have the "master master page" with an example menu code, then have a local "nested master page" only with local app's menu, and have asp files consume that "nested master page".
Is it the only way to do these stuff? Or is there a better way for doing it?
You have many choices with ASP.NET web forms.
If you just need to share server code, such as utility functions, you can add additional classes to your web project and instantiate/call them just like any other code. Classes added in this manner are accessible to all of your pages.
If you want to share server side code across multiple projects, you can create a new project of type "Class Library," compile it, and set a reference to the resulting DLL from all of your web projects.
If you have a page snippet or section (e.g. a menu system) that is common across pages, you have two choices: custom controls or user controls.
Custom Controls: these are controls written from scratch exclusively using code. Essentially, you write a class which extends Control (directly or indirectly), and you take care of everything. You need to write logic to create child controls that you want to use, and you override the Render method to perform rendering. Typically, you build this control into a redistributable assembly (i.e. a DLL), which can then be used by any ASP.NET applications, simply by placing the assembly in the 'bin' directory of the app (or in the Global Assembly Cache).
User Controls: these control are written using an ascx file, which looks much like an aspx page. i.e. you can simply drag and drop the UI elements that you want to use into the design surface. You don't need to worry about control creation, nor about overriding the Render method.
Sounds to me like you want a user control in this scenario. Here is a helpful walkthrough to get you started.
I will echo scheien's sentiment and tell you that you might want to look carefully at MVC, which is totally different but might fit better with your PHP mindset.

Can Sitecore templates be used to store SQL Queries or JavaScript

We are building a Page with dynamic functionality using ASP.NET + Sitecore 7.0.
Is it practical and appropriate use Sitecore templates for:
SQL Stored Procedure Name to be invoked
JavaScript to be invoked
ColumnNames to be used etc (related to coding customization)
Or should these configuration properties remain inside the ASP.NET Project itself?
What is the primary purpose of Data Templates in Sitecore?
Are they for developer customization or customer-level customization?
The purpose of a data template in Sitecore is to define the set of fields for content items which inherit from that template. - Think of a data template as a class and the content items (pages) as instances of that class.
Templates are usually used to define the user-editable content of pages within a site, that being said you can have items to store information which is not managed by regular content editors. The question is where do you draw the line between things which should be put into Sitecore and things which should be a part of the solution. My advice is only put things in Sitecore if they need to be there. If you have to have the ability for editors or admins to configure those settings/properties.
I would say that putting SQL/ColumnNames is probably a bad idea unless you are building some sort of report builder interface in which case it may be essential?
Likewise with placing JavaScript into Sitecore; this can be OK in moderation (e.g. snippets of analytics code which content editors may want control over?). If you're placing whole libraries of JavaScript into Sitecore, you're probably doing it wrong.
One final point to note is findability/re-factorability of code: if you have code spread between Sitecore and your solution, it can make it very difficult to maintain as it is difficult to get a complete overview of code involved.

Generate Asp.Net Bundle based on value in the querystring

I'd like to be able to generate an Asp.Net bundle at runtime but change the contents of it based on the result of a database query.
Here's the scenario: working within a CMS system, I build up a page of content based on lots of little "snippets" of Html, each of which can potentially have their own set of stylesheets and javascript files. Each content page can be created at runtime by the user, so at design time I don't know which assets I will need.
So what I would like to be able to do is something like this:
#Styles.Render("~/contentPage/?id=2")
Where id is the page ID that the snippets are part of. Then, I would like to be able to take that ID, load the page, load the snippet entities which are associated with it, get a set of stylesheet names then produce the bundle.
Is this possible within the current confines of the framework? If not, is it possible to build my own handler for this which leverages the power of the existing framework to do the minification and all the other cool stuff that I get out of the box?
Thanks in advance

ASP.NET MVC application level soft coded settings

I am working on a ASP.NET MVC 2.0 Multi-Presentation web application which would use a common codebase to support different websites. These websites would differ in following aspects:
Each website will have their own headers, footers, images, CSS etc (I guess website specific Master Pages)
Some of the UI elements could be different based on soft-coded settings at website level
What is the best approach to handle these requirements? Should I be storing these website level soft-coded settings in Database or multiple config files? I might have to provide a admin UI to manage these soft-coded settings. How do I access these settings in different layers (MVC, Services, Repositories etc) of my application?
I need suggestions from experts.
Regards,
Alex.
Develop some sort of Presentation Object Model (or possibly Aggregate if you're DDD inclined) and then persist that to the db. It could be per-user or per-site (if it's a multi-tenanted app you're building) and with MVC it would be easy to build Html Helpers that accept these Presentation Model Objects and render out your customized header, css, images etc.
Potential long-shot practical example:
Let's say your 'tenant' is called a Site - you'll prob have a Site object in your model. A property of Site could be a WebPresentation object. WebPresentation may have a series of child objects and lists with methods for reading css, css files (for <link> elements in the document head), images, etc. You may also have a Service or set of Service functions for persisting/validating the WebPresentation objects (or you may try to have this functionality come off the objects themselves- think .Save()). I won't go into how you persist this data to the db (don't want to insult you). As for the Views, you would design them (perhaps Partial Views) such that the custom WebPresentation Html helpers render the customizations out into the views or <head> sections of your MasterPages.
I hope this example is useful.

ASP.NET - best method for website where user can create articles (like a blog)

I'm an ASP.NET newbie, but not so new at programming in general.
I'm creating a commercial website, and I want to allow an admin to add new articles (an article consists of text, images and various properties such as category).
I am trying to decide the optimal Modus Operandi. This site is commercial, so SEO is a major consideration. This means that I want each url to be "unique". That is, if someone navigates to an article about raccoons, he should be redirected to www.mysite.com/articles/raccoons. This means - I can't have one page that loads the appropriate article dynamically a-la AJAX (gotta use deep-linking)
So how exactly do I do this? suppose the admin entered his text, uploaded the images and set the article properties. I create a new subfolder, save the images to the server (I understand that saving images to a DB is a big no-no), their addresses in a DB, and the content itself to the DB. But now what?
How do I go about creating the actual page?
Is there a function for creating a new aspx file? then what about its corresponding cs file? Or is it unwise to use aspx? Maybe plain html? but then how does it work with my site's master page? Or maybe just create another copy of a general aspx file which is populated with an article according to a parameter?
I would like to know what is the "smartest" approach before I dive in too deep.
You can Consider ASP.Net MVC for this. What you need is more like a Content Management System rather than a Blog, as you mentioned an administrator will add articles.
By Using ASP.Net MVC, you have a very clean implementation there, your urls will stay as you need it for SEO, You dont have to create aspx pages on the fly but the framework will let you deal with new urls from your class files.

Resources