繁体   English   中英

我想在.net MVC中的URL中添加破折号,该怎么做?

[英]I want to put dash in URL in .net MVC.How to do it?

我正在使用MVC5,无法在URL中添加破折号。 我想要的是在单词之间加一个短划线。请查看我的工作代码。

我的路线配置为:

            routes.Add(
                  new Route("{MyApple}/{page}",
                       new RouteValueDictionary(
                            new { controller = "MyApple", action = "Index", page = "" }),
                            new HyphenatedRouteHandler())
                  );   

我的控制器是:

public ActionResult Index(string page)
{
    if (System.IO.File.Exists(Server.MapPath("/Views/MyApple/" + page + ".cshtml")))
    {
        string userId = "";
        if (Session["AppId"] != null)
        userId = Session["AppId"].ToString();

         Session["ReturnUrl"] = Url.Action("EmptyTemplate");
         IBaseViewModel model = new BaseViewModel();
         return View(page, model);
     }            
} 

这是我的控制器,参数进入页面。 示例:devpage = aspdotnet,那么它将呈现view = aspdotnet.html

请帮助编辑此内容。 我想用小写字母划线到URL。 示例:假设我有perfectsolution.html页面,然后我

localhost/case-study/perfect-solution

(不要介意我的英语)。将不胜感激。 谢谢

注意:在索引页面中,我正在使用“完美解决方案”传递给视图,但是我想要URL,完美解决方案。

我想要这种类型的URL / case-studies / san-Diego-veg-festival,casestudies是控制器,而san-diego-veg-festival是参数。

根据您的评论,我猜测您正在尝试在CaseStudies控制器的Index方法中传递参数值。

有两种类型的路由可供选择,您可以根据需要选择任何人:

1.基于约定的路由

在您的RouteConfig.cs文件中进行以下更改:

  public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
           name: "CaseStudiesRoute",
           url: "case-studies/{devPage}",
           defaults: new { controller = "CaseStudies", action = "Index", devPage = UrlParameter.Optional }
       );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

控制器:

public class CaseStudiesController : Controller
{

    public ActionResult Index(string devPage)
    {
        return View();
    }

}

现在,使用浏览器将GET请求发送到URL, /case-studies/san-Diego-veg-festival ,它应该可以与基于约定的路由一起使用。

2.属性路由

在您的RouteConfig.cs ,通过添加routes.MapMvcAttributeRoutes();启用属性路由routes.MapMvcAttributeRoutes(); ,现在RouteConfig.cs看起来像:

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapMvcAttributeRoutes();

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

控制器:

public class CaseStudiesController : Controller
{

    [Route("case-studies/{devPage?}")]
    public ActionResult Index(string devPage)
    {
        return View();
    }

}

现在,使用您的浏览器向URL /case-studies/san-Diego-veg-festival发出GET请求,它应该可以与属性路由一起使用。

参考文献

1。

2。

尝试使用路由属性功能

[路线(“研究/完美解决方案”)]

例如:

[Route("study/perfect-solution")]
public ActionResult PerfectSolution()
{
// code here
}

如果要传递参数,则需要指定

例如:

[Route("study/perfect-solution/{parameter}")]
public ActionResult PerfectSolution(string parameter)
{
// code here
}

暂无
暂无

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

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