【问题标题】:How do i change the url name with routing or url rewrite?如何通过路由或 url 重写更改 url 名称?
【发布时间】:2018-09-18 13:49:30
【问题描述】:

当用户浏览它时,这是我的原始网站 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( 名称:“预览网页”, url: "模板/PreviewWebPage/{id}", 默认值:新{控制器=“模板”,操作=“PreviewWebPage”,id =“”} );

【问题讨论】:

  • 您是否阅读过很多关于网络上关于RouteConfig 类的文章?例如codeproject.com/Articles/624181/Routing-Basics-in-ASP-NET-MVC
  • 您可能还想澄清一下您使用的是 asp.net mvc 还是 asp.net webforms。
  • 请分享最小代码示例
  • 当你浏览http://test/Template/PreviewWebPage/Hello时会发生什么?
  • 显示与test/Template/PreviewWebPage/7相同的内容。我只是想将可以是任何 ID 号的单词 7 更改为用户将获得一个表单以在其上键入名称的单词

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


【解决方案1】:

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

【讨论】:

  • 我正在使用windows系统和visual studio 2017来开发这个项目
猜你喜欢
  • 2014-04-23
  • 2011-01-19
  • 2013-01-19
  • 1970-01-01
  • 1970-01-01
  • 2018-04-04
  • 1970-01-01
  • 2015-12-31
  • 1970-01-01
相关资源
最近更新 更多