简体   繁体   中英

Dynamically generate URL from Database in ASP.NET MVC?

Hello to all developers,

I am trying to find some example of Dynamically generate URL(PURL). I have seen one software which generate personalised URL from MySQL database . The only thing is that software is using PHP to generate URL and as a .NET developer I dont know about php. My question is is there a way that I can fetch data from data base and using perticular field i can generate url which is not actually store in my web space..

eeg.
-----database-----

firstname  |  lastname .... 
=========================== 
xyz            abc
pqwert         qweoiuy 
alfa           beta

the URL should be like

http://something.com/somepersonal/xyz_abc.aspx

http://something.com/somepersonal/pqwert_qweoiuy.aspx

http://something.com/somepersonal/alfa_beta.aspx

It is something like personalised page for users or clients. I have template to use as back page but dont know how to plug and dynamically generate URL..

You're thinking of this a little backward. Generally, one wouldn't do this by generating it directly from the database. You would generally do this by routing the url batch /somepersonal/ to some personalpage control, which would then consider everything after it, like xyz_abc to be an argument. Personalpage would then use that argument to customize its display, be it from the database or wherever.

So to clarify, your URL would look something like:

http://something.com/personal/vish_soni
http://something.com/personal/colonel_gentleman
http://something.com/personal/general_specific

But all of these are being routed to Personalpage , which then uses vish_soni , colonel_gentleman , and general_specific as arguments to query the database or do whatever else it needs to do.

Microsoft has an overview of this pattern and routing in general here .

You can create a controller class and call an action method. For your case, you just need to send parameter and then lookup customer info to prepare the content.

 public class HomeController : Controller
{

    public ActionResult Me(string id)
    {
        ViewBag.Message = "Welcome to ASP.NET MVC! "+id;
        var contentobj = repository.GetUSerContent(id);
        return View("Index",contentobj);
    }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM