简体   繁体   English

如何通过路由或URL重写更改URL名称?

[英]How do i change the url name with routing or url rewrite?

This is my original website when user browse it http://test/Template/PreviewWebPage/7 where the 7 is the id that passed into the database and get the data and display the webpage 当用户浏览它时这是我的原始网站http:// test / Template / PreviewWebPage / 7 ,其中7是传递到数据库中并获取数据并显示网页的ID

I would like to ask how can I change the 7 into another name such as http://test/Template/PreviewWebPage/Hello where the word Hello still act as an id which will get the data from database. 我想问一下如何将7更改为另一个名称,例如http:// test / Template / PreviewWebPage / Hello ,其中“ Hello”一词仍然是一个id,将从数据库中获取数据。

The Hello word is also can be set by the user and change it such as Food or any other word which the user input 您还可以由用户设置Hello词,并更改它,例如Food或用户输入的任何其他词

I am using asp.net mvc. 我正在使用asp.net mvc。

Here are my code for the PreviewWebPage 这是我的PreviewWebPage代码

  public ActionResult PreviewWebPage(ShowSectionViewModel model, int id)
    {
        var item = dc.SECTION.Where(x => x.TEMPLATEID == id).OrderBy(x=>x.ORDERINDEX).ToList();
        var item2 = dc.SLIDESHOW.ToList();

        List<slideShowClass> slideShowClassList = new List<slideShowClass>();
        List<sectionClass> sectionClassList = new List<sectionClass>();
        sectionClass sc = new sectionClass();
        slideShowClass ssc = new slideShowClass();
        for (int i=0; i < item.Count(); i++){
            if (item[i].SECTIONTYPE != "Slide Show")
            {

                sc.SECTIONID = item[i].SECTIONID;
                sc.SECTIONIMAGE = item[i].SECTIONIMAGE;
                sc.SECTIONNAME = item[i].SECTIONNAME;
                sc.SECTIONTEXT = item[i].SECTIONTEXT;
                sectionClassList.Add(sc);
            }
            else
            {   
                for (int j = 0; j < item2.Count(); j++) {
                    if (item[i].SECTIONID == item2[j].SECTIONID)
                    {
                        ssc.SECTIONID = item[i].SECTIONID;
                        ssc.IMAGEFILE = item2[j].IMAGEFILE;
                        slideShowClassList.Add(ssc);
                    }

                }
            }
        }

        return View(new ShowSectionViewModel()
        {
            TemplateId = id,
            Sections = item,
            SlideShow = item2,
            sc = sectionClassList,
            ssc=slideShowClassList,

        });

    }

Sorry for my messy question as this is the first time I ask question at stakoverflow 对不起,我的问题很麻烦,因为这是我第一次在stakoverflow上提问

Here are my code for the route config 这是我的路由配置代码

routes.MapRoute( name: "PreviewWebPage", url: "Template/PreviewWebPage/{id}", defaults: new { controller = "Template", action = "PreviewWebPage", id =""} ); route.MapRoute(名称:“ PreviewWebPage”,url:“ Template / PreviewWebPage / {id}”,默认值:new {controller =“ Template”,action =“ PreviewWebPage”,id =“”}));

before start rewriting url you need to set up your work environnement : the a2enmod rewrite could perhaps be usefull in your case : https://www.digitalocean.com/community/tutorials/how-to-rewrite-urls-with-mod_rewrite-for-apache-on-ubuntu-16-04 . 开始重写URL之前,您需要设置工作环境:a2enmod重写可能对您有用: https ://www.digitalocean.com/community/tutorials/how-to-rewrite-urls-with-mod_rewrite- for-apache-on-ubuntu-16-04 (That's for linux on which system are you ?) (那是针对您在哪个系统上的Linux的?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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