简体   繁体   中英

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

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.

Try using Routing Attribute functionality

[Route("study/perfect-solution")]

eg:

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

If you want to pass parameters it need to be specified

Eg:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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