开发者

Serving HTML or ASPX files with ASP.NET MVC

开发者 https://www.devze.com 2022-12-17 02:55 出处:网络
I want to implemented URL like : www.domain.com/Business/Manufacturing/Category/Product1 This will give the details of speci开发者_C百科fic product(such as specifications).

I want to implemented URL like :

www.domain.com/Business/Manufacturing/Category/Product1

This will give the details of speci开发者_C百科fic product(such as specifications). Since the list of Categories & Products in each Category are limited, I thought it is not worth to use database for products.

So I want to implement it using the HTML or ASPX files. Also I want the URL to be without the files extention(i.e. URL's should not have .html or .aspx extension)

How can I implement it with ASP.NET MVC? Should I use nested folder structure with HTML/ASPX files in it, so that it corresponds to URL? Then how to avoid extensions in URL? I am confused with these thoughts


Asp.net Mvc uses the routing library from Microsoft. So it is very easy to get this kind of structure without thinking about the folder structure or the file extensions. With asp.new mvc you do not point a request at a specific file. Instead you point at a action that handles the request and use the parameters to determine what to render and send to the client. To implement your example you can do something like this:

_routes.MapRoute(
            "Product",
            "Business/Manufacturing/Category/Product{id}",
            new {controller = "Product", action = "Details", id = ""}
            );

This route will match the url you described and execute the action named "Details" on a controller named "ProductController" (if you are using the default settings). That action can look something like this:

public ActionResult Details(int id) {
    return View(string.Format("Product{0}", id);
}

This action will then render views depending on what id the product have (the number after "Product" in the end of your example url). This view should be located in the Views/Product folder if you use the default settings. Then if you add a view named "Product1.aspx" in that folder, that is the view that will be rendered when you visit the url in your example.

All tough it is very possible to do it that way I would strongly recommend against it. You will have to do a lot of duplicated work even if you only have a few products and use partial views in a good way to minimize the ui duplications. I would recommend you use a database or some other kind of storage for you products and use a single view as template to render the product. You can use the same route for that. You just edit your action a little bit. It can look something like this:

public ActionResult Details(int id) {
    var product = //get product by id from database or something else
    return View(product);
}

This way you can strongly type your view to a product object and you will not have that much duplication.

The routing engine is very flexible, and when you have played around with it and learned how it works you will be able to change your url in just about any way you want without changing any other code or moving any files.


If you're not ready to dive into ASP.Net MVC, you can still get the nice URLs by using URL Rewriting for ASP.Net. That'd be simpler if you're already familiar with ASP.Net WebForms. The MSDN article on URL Rewriting should be a good start:

http://msdn.microsoft.com/en-us/library/ms972974.aspx

I'd be really sure you won't eventually have more products before deciding not to use a database. Both MVC and WebForms would allow you to make one page that dyamically shows a product and still have a nice URL- plus you might save yourself development time down the road. Something to think about.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号