如何使用路由或网址重写更改网址名称?

时间:2018-04-09 08:11:43

标签: c# asp.net url-rewriting routing url-routing

当用户浏览时,这是我的原始网站 http://test/Template/PreviewWebPage/7 其中7是传入数据库并获取数据并显示网页的id

我想问一下如何将7更改为另一个名称,例如 http://test/Template/PreviewWebPage/Hello其中Hello仍然可以作为一个id来获取数据库中的数据。

Hello字也可以由用户设置并更改,例如Food或用户输入的任何其他单词

我正在使用asp.net mvc。

以下是我的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,

        });

    }

很抱歉我的问题很乱,因为这是我第一次在stakoverflow上提问“

以下是我的路由配置代码

routes.MapRoute(                name:&#34; PreviewWebPage&#34;,                网址:&#34;模板/预览网页/ {id}&#34;,                默认值:new {controller =&#34; Template&#34;,action =&#34; PreviewWebPage&#34;,id =&#34;&#34;}            );

1 个答案:

答案 0 :(得分:0)

在开始重写网址之前,您需要设置工作环境: 在你的情况下,a2enmod重写可能是有用的: https://www.digitalocean.com/community/tutorials/how-to-rewrite-urls-with-mod_rewrite-for-apache-on-ubuntu-16-04。 (那是关于Linux的系统吗?)

相关问题