0

Before Posting This Question I Searched And Tried Out Many Examples As Well.

For My Problem There Was A Similar POST But It is Not Working Too.

Here Is The Structure Of MY Whole APPLICATION:

Here Is The Structure Of ADMIN AREA :

Areas Structure

Project Has Two Applications Now One For Client And Another For Admin : Admin Is Managed Through Areas

I Have Used Angular 1.3.5 And It is Used Inside The Areas.

The Landing Page Is Index Page of Home Controller Path :- Areas/Admin/Views/Home/Index.cshtml

Layout is _Layout Path : Areas/Admin/Views/Shared/_Layout.cshtml

RouteConfig.cs File Of Inside App_Start Folder :

namespace StockManagment
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces : new []{"StockManagment.Controllers"}
            );
        }
    }
}

AdminAreaRegistration.cs

namespace StockManagment.Areas.Admin
{
    public class AdminAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "Admin";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Admin_default",
                "Admin/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional },
                new [] {"StockManagment.Areas.Admin.Controllers"}
            );
        }
    }
}

HTML5MODE For Removing # Form URL

myApp.config(["$locationProvider", function ($locationProvider) {
    $locationProvider.html5Mode(true);
}]);

Base Tag In _Layout Page:

<base href="/">

Web.config Code Of Admin Areas

<rewrite>
  <rules>
    <rule name="Main Rule" stopProcessing="true">
      <match url=".*" />
      <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory"   negate="true" />
      </conditions>
      <action type="Rewrite" url="/" />
    </rule>
  </rules>
</rewrite>

When I Try To Access Admin Side Using This URL: http://localhost:8917/Admin/Home/Index

It Loads The Website At : localhost:8917/

And If I Refresh The Page It Loads The Original Content i.e The Client Side Home Page.

Where Am I Going Wrong In This ?

Thanks In Advance...

Edit 1 :

i removed the rewrite code in web.config i used it because i saw it in another post

before using

myApp.config(["$locationProvider", function ($locationProvider) {
    $locationProvider.html5Mode(true);
}]);

and

<base href="@Request.Url.AbsolutePath" />

to access admin side home page the url was :- http://localhost:8917/Admin/Home/Index#/

after adding the above code it is :- http://localhost:8917/Admin/Home/

accessing categories page

categories before code :- http://localhost:8917/Admin/Home/Index#/Categories

categories after code :- http://localhost:8917/Admin/Home/Categories

when i refresh it gives error

Server Error in '/' Application. The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /Admin/Home/Categories

Community
  • 1
  • 1
3gth
  • 550
  • 5
  • 23
  • I think it is because of the base url, try using content.url - http://stackoverflow.com/questions/23998867/angular-app-on-iis-8-x-virtual-directory-has-strange-url-behavior/23999511#23999511 or base href="@Request.Url.AbsolutePath"/> – Ziv Weissman Jun 13 '16 at 14:24
  • @ZivWeissman it works and removes the # from url but when i refresh it gives error – 3gth Jun 13 '16 at 15:30
  • I have the same problem, did you find a solution ? – kbaccouche Sep 19 '16 at 09:25
  • @kbaccouche no solution yet. – 3gth Sep 20 '16 at 11:11
  • I got this to work by creating an Action in my controller for that URL which redirects to the main view like this `return View("../Home/Index");` and then put `/` instead of `@Request.Url.AbsolutePath` in the base element. Hope it helps. – kbaccouche Sep 20 '16 at 12:53

2 Answers2

0

I'm not sure why you're using the rewrite rule, I can imagine this would interfere with the routing you are trying to do with MVC. I would suggest removing the url rewrite rule from the web.config.

If you want a collection of routes to map to the Index action in the admin area (so that various urls go to your angular SPA), use a wildcard MVC route.

e.g.

// Route override to work with Angularjs and HTML5 routing
    context.MapRoute(
        name: "Application1Override",
        url: "Application1/{*.}",
        defaults: new { controller = "Application1", action = "Index" }
    );
MIP1983
  • 1,354
  • 1
  • 15
  • 25
  • i tried you way in the admin area registration it is not working it gives error too – 3gth Jun 13 '16 at 15:27
  • admin is an area in the application where i am using angularjs it has its own controllers models and views and routing way when using base href and html5mode it loads the admin area on the root side where client should load and when refreshing it load original client content – 3gth Jun 13 '16 at 15:28
  • Taking angular out of the equation for a second, can you confirm if your wildcard route works in MVC. Do all routes to Admin/* take you to your index page (with the angular part disabled for now)? – MIP1983 Jun 13 '16 at 16:05
  • Also, I personally find attribute routing a bit easier than centrally defining routes, i.e. [Route("admin/{*catchall}"]. – MIP1983 Jun 13 '16 at 16:11
  • i am not sure what you mean to say by disabling angular if meant to comment the html5mode and base tag and use the context map route well i tried it and its not working for me – 3gth Jun 13 '16 at 16:12
  • I simply mean testing whether the MVC routing configuration works on it's own, without trying to load the angular app on the index page. Once you know all routes are serving up that view (just put a simple hello message on it), then you can add your angular app back into the mix. – MIP1983 Jun 15 '16 at 10:47
0

In your initial problem you had

<base href="/" />

which tells angular that the basic routing is "/", so it will change the href to the base ("/") when you reload the page, MVC will take you to "/" which is your root action.

When you are changing to -

<base href="@Request.Url.AbsolutePath" />

angular will know the base of this page is your current {action}/{controller}

Now you have another problem with the routing:

namespace StockManagment.Areas.Admin
{
    public class AdminAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "Admin";
            }
        }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{id}", // your current url= /Admin/Home/Categories -- which means id = Categories
            new { action = "Index", id = UrlParameter.Optional },
            new [] {"StockManagment.Areas.Admin.Controllers"}
        );
    }
}}

If you don't need ID you can use this code:

public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{*angular}",
            new { action = "Index",
            new [] {"StockManagment.Areas.Admin.Controllers"}
        );
    }
3gth
  • 550
  • 5
  • 23
Ziv Weissman
  • 4,400
  • 3
  • 28
  • 61