I want to set content and script for each area in ASP.NET MVC.

Asked 2 years ago, Updated 2 years ago, 80 views

Developed in ASP.NET MVC5 (Visual Studio 2013 Update 4) environment.

Content (e.g., css) and Script (e.g., Javascript) usually create and expand folders directly under the project.
Is it possible to place this for each area?

In the environment I tried,

Area
 -hogehoge
  -Content
   - Style.css

and

@Url.Content (~/hogehoge/Content/Style.css) Also @ System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl (~/hogehoge/Content/) There was also an error because the file could not be viewed.

.net c# asp.net razor

2022-09-30 19:37

1 Answers

Isn't it Area s instead of Area?


in the following configurations: SolutionExplorer

Style.css is

body{
    background-color:skyblue;
}

@ If you specify directly in Url.Content, for example, to prepare a link tag in _Layout.cshtml, you can specify:
I think you can specify the same without using Layout.

<link href="@Url.Content("~/Areas/hogehoge/Content/Style.css")"rel="stylesheet" type="text/css"/>

AffectedPage
Page with style

However, it would be better to add bundles as much as possible and add them in @Styles.Render.
To do so, do the following:

First, open App_Start/BundleConfig.cs.

BundleConfig.cs

Add the following bundles.Add

bundles.Add(new StyleBundle("~/Areas/hogehoge/Content/css").Include(
          "~/Areas/hogehoge/Content/Style.css"
    ));

Change the <link href="@Url.Content line that you just added as follows:

@Styles.Render("~/Areas/hogehoge/Content/css")

If you added a style, increase the Include argument in Bundles.Add.
(bundles.Add (new StyleBundle("~/Content/css"). that you may already have will be helpful.)

Incidentally, BundleTable.Bundles.ResolveBundleUrl( for example

<!--
   @ System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/Areas/hogehoge/Content/cs") 
-->

HTML will be printed as follows, so

<!--
    /Areas/hogehoge/Content/css?v=yhOQn7MXW55kWVM6x07Bf3QlCaPBa716IIGs-NlMwiA1 
-->

I think I can get the target URL properly.


2022-09-30 19:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.