0

I want to create a web application using C#/MVC5 with the following folder structure, where my client sources are separated from the server's ones

  • Controllers
    • HomeController.cs
    • FooApiController.cs
  • Client
    • assets ( folder )
      • images ( folder )
        • logo.png
      • font.ttf
    • components ( folder )
      • foo ( folder )
        • fooService.js
    • index.cshtml
    • web.config (EDIT)

I want to be able to call my differents files in the client folder, without the folder 'client',e.g:

<img src="assets/images/logo.png" /> 

instead of

<img src="client/assets/images/logo.png" />

Moreever, I want to route all the others files to client/index.cshtml. I though about adding a route like this :

routes.MapRoute(
    name: "Default",
    url: "{*anything}",
    defaults: new { controller = "Home", action = "Index" }
);

HomeController.cs

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View("~/client/index.cshtml");
    }
}

And adding rules as specified in this post for each folder in my client folder. e.g :

<rule name="client" stopProcessing="true">
      <match url="^assets/?(.*)$" />
      <action type="Rewrite" url="/client/assets/{R:1}" />
</rule>

The problem with this is that whenever I tried to get my image

http://localhost:49312/client/assets/images/logo.png

or

// returns to http://localhost:49312/client/assets/images/logo.png
http://localhost:49312/assets/images/logo.png 

I get a 'Ressource not found' error

What is the proper way to go ?

Community
  • 1
  • 1
KANAX
  • 275
  • 2
  • 13

2 Answers2

0

I just found out why it wasn't working : I copied the web.config file from the default Views folder to my client folder.

There was an blocking handler in this file:

<system.webServer>
    <handlers>
       <remove name="BlockViewHandler"/>
       <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
</system.webServer>

I did everything I said in my initial post plus removing this handler, and it works great now

KANAX
  • 275
  • 2
  • 13
0

Have you heard about MVC Areas?

https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/areas

http://www.itorian.com/2013/10/area-in-mvc-5-with-example-step-by-step.html

https://msdn.microsoft.com/en-us/library/ee671793(v=vs.100).aspx

Anestis Kivranoglou
  • 7,728
  • 5
  • 44
  • 47
  • It could have been a solution, but I want to reuse my client sources in a different project ( UWP html/js ) – KANAX Feb 13 '17 at 12:30