Search code examples
c#asp.net-mvcasp.net-mvc-routingmaproute

About ASP.NET MVC route


Such as I get a filter model:

public class  Filter
{
   public int  Id{get;set;}
   public  string Name{get;set;}
  public  DateTime CreateTime{get;set;}
}

And there is a SearchController action like:

public  ActionResult  Search(Filter  filterModel)
{
      List<Model>  model =SampleBll.get(filterModel)
}

so the question is.How to configure URL like

/Search/{Filter.Id}_{Filter.Name}_{Filter.CreatTime}/

Thank you for your help


Solution

  • You can treat {Filter.Id}_{Filter.Name}_{Filter.CreatTime} as string filter and parse it in your controller.

    public ActionResult Search(string filter)
    {
       var parts = filter.Split("|".ToCharArray());
    
       Filter model = new Filter();
       model.Id = Int32.Parse(parts[0]);
    
       // ...
    }