2012-02-09 34 views
0

這裏的控制器得到URI在asp.net mvc的動作

public class FeedController : ControllerBase 
    { 

     public RssActionResult Rss() 
     { 
      //........ 

      List<SyndicationItem> items = new List<SyndicationItem>(); 
      var photosList = Facade.Photos.Get() 
       .Select(x => 
        new SyndicationItem(
         x.Title, 
         x.PreviewPath, 
         new Uri("http://site.com/"+CurrentLocale.CultureName+"/photos/show/" + x.ID + ".html"), x.ID.ToString(), 
         DateTime.UtcNow) 
         ); 
      //........ 
      return new RssActionResult() { Feed = feed }; 
     } 

    } 

的如何避免URI的硬編碼的方法裏面?

UPDATE

routes.MapRouteLowercase(
       "Photos_Route", 
       "{culture}/photos/{action}/{id}.html", 
       new { controller = "photos", action = "show", culture = defaultCulture } 
       ); 
+0

這是什麼網址?它是你網站的一部分嗎?或者它是一些外部網址?如果它是你網站的一部分,你的路線定義是怎樣的?或者它只是一個靜態文件? – 2012-02-09 22:18:57

+0

這是我的網站的網址。 – Alexandre 2012-02-10 02:02:52

回答

1

如果URI是在自己的網站資源,如果你有這方面規定的路線,你可以使用類似這樣的東西:

Url.RouteUrl(new RouteValueDictionary{ {"controller","Photo"}, {"action","show"}, {"Id",x.ID} }) 

RouteUrl方法有幾個重寫。

否則,如果該URI是某個其他站點上的資源,則可以將URI格式存儲在web.config中,並使用string.Format(urlFormatFromWebConfig,x.ID)來生成URL。

編輯:更具體的,現在你跟你的路由更新:

new Uri(Url.RouteUrl("Photos_Route", new RouteValueDictionary{ {"culture", CurrentLocale.CultureName}, {"ID", x.ID.ToString()} }); 
+0

您的編輯存在錯誤。 – Alexandre 2012-02-10 04:01:43

+0

感謝您指出。我認爲現在是固定的。 – 2012-02-10 04:08:31