MVC控制器

时间:2012-03-06 14:15:55

标签: c# asp.net-mvc asp.net-mvc-3 razor

此时我的控制器正在以某种方式工作,此时网址出现Home/Collections/Collection?id=1,这是我的预期功能。但是,我现在想让网址更友好。例如,我希望它成为Home/Collections/Summer

我的CollectionsController中有以下代码:

    public ActionResult Index()
    {
        return View(Helper.Helper.ResolvePath("Collections"));
    }


    //
    // GET: /Collections/Collection?id=1
    public ActionResult Collection(int id)
    {
        var collectionModel = ds.GetCollection(id);
        return View(Helper.Helper.ResolvePath("Collection"), collectionModel);
    }

我需要更改什么才能获得理想的结果?没有为每个集合单独ActionResult(因为它永远不会是固定数字)?

这是我的Global.asax

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

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

    }

5 个答案:

答案 0 :(得分:2)

您可以定义类似的路线,

 routes.MapRoute(
            "Collections", // Route name
            "Collections/{name}/{id}", // URL with parameters
            new { controller = "Collections", action = "Collection" ,id = UrlParameter.Optional } // Parameter defaults
        );

现在在CollectionsController中,

public class CollectionsController : Controller
{      
    public ActionResult Collection(string name)
    {
      /// Your Logic here
      /// name is required parameter
      /// optionally you can add an id parameter
        return View();
    }

}

现在您的网址将像“http:// localhost:98765 / Collections / Summer”

但我不明白为什么你需要在网址主页

答案 1 :(得分:1)

鉴于以下enum

public enum Seasons { Summer = 1, Fall = 2, Winter = 3, Spring = 4 }

为路线映射添加自定义路线:

routes.MapRoute(
    "Default", // Route name
    "Home/Collections/{action}/{season}", // URL with parameters
        new { controller = "Collections",
              action = "Collection",
              season = UrlParameter.Optional } // Parameter defaults
);

控制器操作:

public ActionResult Collection(Season season)
{
    /* ... code ... */
}

答案 2 :(得分:0)

id参数更改为字符串,并执行查找相应ID所需的任何数据库查询。

答案 3 :(得分:-1)

除了Manas'答案之外,您还可以在Collection上添加一个“字段/列/属性”来保存URL友好名称。

并非所有名称都是URL有效名称(例如,如果您的收藏品是“50%折扣!!”,您肯定会在URL上发现意外结果http://server/Collections/50%关闭!!)

所以你可能想尝试在你的Collection中添加一个“UrlFriendlyName”,并从官方名称自动生成它:

public class Url
{
    public static string GetUrlFriendlyName(string name)
    {
        //Unwanted:  {UPPERCASE} ; / ? : @ & = + $ , . ! ~ * ' ( )
        name = name.ToLower();
        //Strip any unwanted characters
        name = Regex.Replace(name, @"[^a-z0-9_\s-]", "");
        //Clean multiple dashes or whitespaces
        name = Regex.Replace(name, @"[\s-]+", " ");
        //Convert whitespaces and underscore to dash
        name = Regex.Replace(name, @"[\s_]", "-");
        name = Regex.Replace(name, @"-+", "-");
        return name;
    }
}

这样,您就可以根据网址友好名称查询您的收藏集(理想情况下,它会是唯一的,但会有很多其他问题发挥作用...就像在网址中添加ID一样(http://server/Collections/Summer/1或者将随机数添加到URL友好名称,例如http://server/Collections/Summer-123,或者只是让创建集合的用户指定一个唯一的URL友好名称,从自动生成的名称开始,但如果用户不是唯一的则会失败回来。“ p>

答案 4 :(得分:-2)

要有一个像这样的Home / Collections / Summer这样的URL你需要有一个名为“Summer”的方法,你可以让它将ActionResult返回给Summer的相应id,如下所示:

public ActionResult Summer() {
   return Collection(1); //where id 1 corresponds to summer
}
相关问题