2016-09-20 51 views
1

我想使「模板系統」的東西,它可能允許用戶更改網站模板。例如,在管理面板中。在不同的文件夾中的Css

我現在的文件結構

-Controllers 
-Models 
-Views 
--Templates 
---DefaultTemplate 
----css 
-----style.css 
----Index.cshtml 

在默認的控制器(家)我檢查當前模板的名稱,並顯示右視圖

private ViewResult TemplateView(string viewName) 
{ 
    return View($"~/Views/Templates/{GlobalSettings.CurrentTemplate}/{viewName}.cshtml"); 
} 

還有一些模板視圖(Index.cshtml

..... 
<link rel="stylesheet" href="~/Views/Templates/DefaultTemplate/css/style.css" type="text/css" /> 
..... 

但我不知道如何寫正確的路徑到我的CSS \ js \圖像在正確的模板e forder。

我已經試過

./css/style.css 

@Url.Content("/css/style.css") 

但在結果HTML時,它就像mysite.com/css/style.css,所以我得到了404(未找到)

我也試過這種

~/Views/Templates/DefaultTemplate/css/style.css 

在這種情況下,我也得到了404。

那麼,有人可以幫助我嗎?如何寫正確的URL到CSS,圖像,HIS等?

回答

1

在/瀏覽文件夾中的web.config文件限制默認情況下,所有訪問文件的文件夾中:

代碼添加到您的web.config文件:

<httpHandlers> 
    <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/> 
</httpHandlers> 

注:這不是很好練習將css文件存儲在視圖文件夾中。

+0

我可以只允許指定的路徑訪問?不是所有視圖文件夾? –

+0

參考[this](http://stackoverflow.com/questions/604883/where-to-put-view-specific-javascript-files-in-an-asp-net-mvc-application),但爲什麼你存儲在裏面查看文件夾? –

+0

用戶友善。 「只需將包含視圖,css,js,圖片的文件夾放入模板文件夾並在設置中選擇它即可」 - 無需將樣式放入「css/template」,在「js/template」中放置條紋等。 –

相關問題