我想在.net MVC中的URL中添加破折号,该怎么做? [英] I want to put dash in URL in .net MVC.How to do it?

查看:111
本文介绍了我想在.net MVC中的URL中添加破折号,该怎么做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我的路由配置是:

  route.Add(新路线("{MyApple}/{page}",新的RouteValueDictionary(new {controller ="MyApple",action ="Index",page ="}),新的HyphenatedRouteHandler())); 

我的控制器是:

 公共ActionResult索引(字符串页){如果(System.IO.File.Exists(Server.MapPath("/Views/MyApple/" +页面+".cshtml"))){字符串userId =";如果(Session ["AppId"]!= null)userId = Session ["AppId"].ToString();Session ["ReturnUrl"] = Url.Action("EmptyTemplate");IBaseViewModel模型=新的BaseViewModel();返回View(页面,模型);}} 

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

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

localhost/case-study/perfect-solution

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

注意:在索引页面中,我正在使用完美解决方案"传递给视图,但我想输入URL,即完美解决方案.

解决方案

我想要这种类型的URL/case-studies/san-Diego-veg-festival,案例研究是控制者,而san-diego-veg-festival是参数.

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

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

1.基于约定的路由

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

 公共静态无效RegisterRoutes(RouteCollection路由){route.IgnoreRoute("{resource} .axd/{* pathInfo}");route.MapRoute(名称:"CaseStudiesRoute",url:案例研究/{devPage}",默认值:new {controller ="CaseStudies",action ="Index",devPage = UrlParameter.Optional});route.MapRoute(名称:默认",url:"{controller}/{action}/{id}",默认值:new {controller ="Home",action ="Index",id = UrlParameter.Optional});} 

控制器:

 公共类CaseStudiesController:控制器{公共ActionResult索引(字符串devPage){返回View();}} 

现在,使用您的浏览器向URL发出 GET 请求,/case-studies/san-Diego-veg-festival ,它应可用于基于约定的路由.

2.属性路由

在您的 RouteConfig.cs 中,通过添加 routes.MapMvcAttributeRoutes(); 启用属性路由,现在 RouteConfig.cs 如下:

 公共静态无效RegisterRoutes(RouteCollection路由){route.IgnoreRoute("{resource} .axd/{* pathInfo}");route.MapMvcAttributeRoutes();route.MapRoute(名称:默认",url:"{controller}/{action}/{id}",默认值:new {controller ="Home",action ="Index",id = UrlParameter.Optional});} 

控制器:

 公共类CaseStudiesController:控制器{[Route("case-studies/{devPage?}")]公共ActionResult索引(字符串devPage){返回View();}} 

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

参考

1.

2.

I am working with MVC5 where I am unable to put dashes in the URL. What I want is to put a dash between the words.Kindly view my working code.

My Route Config is:

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

And My controller is :

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);
     }            
} 

This is my controller where the parameters go to page. Example: devpage = aspdotnet then it will render the view = aspdotnet.html

Please help with this edited content. I want to dash to the URL with lowercases. Example: Suppose I have perfectsolution.html page then i

localhost/case-study/perfect-solution

(Don't mind my English).Help will be appreciated. Thanks

Note: in the index page, I` am using "perfect solution" to pass to the view but I want in URL, perfect-solution.

解决方案

I want this type URL /case-studies/san-Diego-veg-festival , casestudies is controller and san-diego-veg-festival is parameter.

As per your comments , I made a guess that you are trying to pass parameter value in CaseStudies controller's Index method.

Two types of Routing to choose from, you can choose anyone as required:

1. Convention-based Routing

In Your RouteConfig.cs file make these changes:

  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 }
        );
    }

Controller:

public class CaseStudiesController : Controller
{

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

}

Now using your browser give a GET request to URL , /case-studies/san-Diego-veg-festival , It should work with Convention-based Routing.

2. Attribute Routing

In Your RouteConfig.cs enable Attribute Routing , by adding routes.MapMvcAttributeRoutes(); , Now RouteConfig.cs looks like:

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 }
        );
    }

Controller:

public class CaseStudiesController : Controller
{

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

}

Now using your browser give a GET request to URL , /case-studies/san-Diego-veg-festival , It should work with attribute routing.

References

1.

2.

这篇关于我想在.net MVC中的URL中添加破折号,该怎么做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆