2014-06-08 21 views
0

嗨由我試圖建立一個角App和我在ASP.NET MVC下面的情況。 我已經在該項目的根目錄,其中包含以下創建的應用程序文件夾:呼籲文件在了錯誤的文件路徑

  • 風格的文件夾
  • 腳本文件夾
  • images文件夾
  • index.html文件

然後,而不是在我的HomeController指數操作返回查看我返回以下:

public ActionResult Index() 
{ 
     return File(Server.MapPath("/App/index.html"), "text/html"); 
} 

中的index.html具有以下樣式補充說:

<link rel="stylesheet" href="styles/main.css"> 

當我運行的index.html被檢索到的應用程序,但我得到的檢索式的文件裏的錯誤:

http://localhost:57826/styles/main.css 

出於某種原因,而不是看它在以下路徑http://localhost:57826/App/styles/main.css它看起來在上面提到的路徑。

然後我tryed攔截來電,並通過創建這樣一個自定義的路由器得到正確的路徑文件:

routes.MapRoute(
      name: "Style", 
      url: "styles/{*path}", 
      defaults: new { controller = "Www", action = "Style" } 
     ); 

public class WwwController : Controller 
{ 
    public ActionResult Style(string path) 
    { 
     byte[] content = this.GetContent(this.CorrectPath(path, "css")); 
     var result = new FileContentResult(content, this.GetContentTypeFor(path)); 
     return result; 
    } 

    private string CorrectPath(string path, string folder) 
    { 
     return Server.MapPath("/App/" + folder + "/" + path); 
    } 

    private byte[] GetContent(string path) 
    { 
     byte[] readAllBytes = System.IO.File.ReadAllBytes(path); 
     return readAllBytes; 
    } 

    private string GetContentTypeFor(string path) 
    { 
     return System.Web.MimeMapping.GetMimeMapping(Path.GetFileName(path)); 
    } 
} 

但我的解決方案似乎沒有work.The風格的動作並沒有被調用。誰能告訴我,我怎麼能解決我的問題

回答

0

爲什麼你打破了不讓你的觀點做演示工作MVC模式?只要做到這一點:

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

然後把你需要進入查看/主頁/ Index.cshtml任何內容:

<link rel="stylesheet" href="styles/main.css"> 
@* Add link tags, javascript, etc. *@ 

或者只是使用香草ASP.NET網站。

+0

同樣的問題堅持使用和MVC文件的事件視圖...無論如何,這將是一個Angular應用程序,我只需要啓動HTML文件....我仍然需要和ASP.NET MVC應用程序,因爲我將創建ASP .NET Web.API服務 – aleczandru