1

I have MVC website where i am trying to make absolute URL with url.action but not succeed yet.

Here is code i am using:

 <li><a href="@Url.Action("Index", "Home")">Home</a></li>
 <li><a href="@Url.Action("Contact","Home")">Contact</a></li>

Both Home and Contact are in same View.

So here is the error description: When I run the website it shows this url and open website perfectly. Now if I click on "Contact" link it shows correct URL:

 http://localhost:5423/Home/Contact

After that I am clicking on "Home" Link it gives me wrong URL:

http://localhost:5423/Home/Home/Index

Correct Url should be this:

http://localhost:5423/Home/Index

I don't know why its retaining "Home" in URL.

This is what I wrote in Global.asax.vb

Public Class MvcApplication
    Inherits System.Web.HttpApplication

    Sub Application_Start()
        AreaRegistration.RegisterAllAreas()

        WebApiConfig.Register(GlobalConfiguration.Configuration)
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters)
        RouteConfig.RegisterRoutes(RouteTable.Routes)
        BundleConfig.RegisterBundles(BundleTable.Bundles)
        AuthConfig.RegisterAuth()
    End Sub
End Class

Here is the code for RouteConfig:

 Public Class RouteConfig
 Public Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
 routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
 routes.MapRoute( _
 name:="Default", _
 url:="{controller}/{action}/{id}", _
 defaults:=New With {.controller = "Home", .action = "Index", .id =  UrlParameter.Optional} _
    )
 End Sub
 End Class

This is only the problem I am facing.

Please give your views and suggestions.

Tieson T.
  • 20,774
  • 6
  • 77
  • 92
user1638889
  • 41
  • 1
  • 10
  • 2
    can you show your route config? – Ehsan Sajjad Apr 20 '15 at 12:13
  • @EhsanSajjad are you asking for webconfig or global? – user1638889 Apr 20 '15 at 12:16
  • The `RouteConfig.cs` file. If you don't have one of those, there should be a bunch of route config related code in the `Application_Start()` method found in `Global.asax.cs`. – Jason Evans Apr 20 '15 at 12:21
  • yes it is in global file: Public Class MvcApplication Inherits System.Web.HttpApplication Sub Application_Start() AreaRegistration.RegisterAllAreas() WebApiConfig.Register(GlobalConfiguration.Configuration) FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters) RouteConfig.RegisterRoutes(RouteTable.Routes) BundleConfig.RegisterBundles(BundleTable.Bundles) AuthConfig.RegisterAuth() End Sub End Class – user1638889 Apr 20 '15 at 12:22
  • Can you show all the calls to `.MapRoute()` method in your code? That will help us understand more about the routes are setup in your project. – Jason Evans Apr 20 '15 at 12:24
  • @user1638889 Edit that into your question, not as a comment - it's unreadable in there. – DavidG Apr 20 '15 at 12:25
  • @DavidG i edited my question.Please check now. – user1638889 Apr 20 '15 at 12:28
  • Can you show the code that's in `RouteConfig.RegisterRoutes(RouteTable.Routes)` please? – Jason Evans Apr 20 '15 at 12:29
  • @JasonEvans there is no method .MapRoute in my code. – user1638889 Apr 20 '15 at 12:30
  • There will be in `RouteConfig.RegisterRoutes()`. – Jason Evans Apr 20 '15 at 12:33
  • @JasonEvans i am searching this method in my code.but there is no method. – user1638889 Apr 20 '15 at 12:36
  • There will be separate file in you App_Start>>Routeconfig.cs, please show it here – Ubiquitous Developers Apr 20 '15 at 12:40
  • @JasonEvans question is updates please check now. Thanks – user1638889 Apr 20 '15 at 14:43
  • OK that looks fine. Can you do the same with `AreaRegistration.RegisterAllAreas()` please? What is the content of that method? – Jason Evans Apr 20 '15 at 15:07
  • @JasonEvans there are only 5 inside App_Start folder. 1)WebApiConfig,vb 2)FilterConfig.vb 3) RouteConfig.vb 4)BundleConfig 5)AuthConfig.vb There is no AreaRegistration.RegisterAllAreas() inside App_Start folder.Please suggest. thnaks – user1638889 Apr 20 '15 at 15:16
  • In the Global.asax.cs file you have this line `AreaRegistration.RegisterAllAreas()` so it must be getting the areas routes from there. If that code is not in App_Start, it must be somewhere else. – Jason Evans Apr 20 '15 at 15:20
  • @JasonEvans i am trying to find RegisterAllAreas() by F12 and its going to Object Brower. – user1638889 Apr 20 '15 at 15:32
  • @JasonEvans please suggest what to do now? – user1638889 Apr 20 '15 at 16:36
  • In your code, right-click on `RegisterAllAreas()` and choose `Go to Definition`. That will take you to the source code for that method. In there you will see calls to `MapRoute()` which should help us clarify what routes have been configured for your `user` area. – Jason Evans Apr 20 '15 at 18:50
  • @JasonEvans yes its redirecting me Object.Browser..no code for this method. – user1638889 Apr 20 '15 at 18:54
  • That's weird. Is there no file in your project called `AreaRegistration.cs`, or a `.cs` file that contains the type `AreaRegistration`? – Jason Evans Apr 20 '15 at 18:59
  • @JasonEvans yes really no – user1638889 Apr 20 '15 at 19:01
  • what should you suggest now? – user1638889 Apr 20 '15 at 19:03
  • Sorry I'm thinking of the wrong thing. You're right, that type is not going to be in your project. – Jason Evans Apr 20 '15 at 19:04
  • The only thing I can suggest is to look at [this post](http://stackoverflow.com/questions/13454699/how-to-register-areas-for-routing) and copy the `AdminAreaRegistration` class, but call it `UserAreaRegistration` and change the other parts accordingly. That way you can create your own routes for the `user` area. – Jason Evans Apr 20 '15 at 19:05

1 Answers1

1

Right, I think I was targeting the wrong thing. In your RouteConfig.cs file, ad new routes for Home/Index and Home/Contact (apologies for the C#, I don't use VB.NET anymore):

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

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

    routes.MapRoute(
        name: "ContactLink",
        url: "Home/Contact",
        defaults: new { controller = "Home", action = "Contact" }
    );

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

In your view, create the links like this:

<li><a href="@Url.RouteUrl("HomeLink")">Home</a></li>
<li><a href="@Url.RouteUrl("ContactLink")">Contact</a></li>

Doing this should fix your url issue. Why Home is appearing twice is unclear to me, but creating your own routes should help.

Jason Evans
  • 28,906
  • 14
  • 90
  • 154
  • Jason thanks alot it really helps me to fix issue.if you are available now then i want to ask you one simple thing? – user1638889 Apr 21 '15 at 22:30
  • Is this something you could ask in a new question? If you create a new question, and add another comment to here, I will see that in my notifications and I could have a look at what else you need to ask. – Jason Evans Apr 22 '15 at 07:13
  • thanks.all issue related to route are resolves now.Now there is a minor issue.I am creating report in my same project.It works well with localhost beofre.Thist report page is normal .aspx page and its outside views. i am using this jquery to show report $('#frameReport').attr('src',"Report.aspx?InvoiceID=" + $('#InvoiceID').val()); $('#divframe').show(); it was working before but now when i am clicking on "report" button it need to redirect to this url Report.aspx?InvoiceID='123' but its retaining me on view like this.http://192.168.1.100/InvoiceReport/Index. – user1638889 Apr 22 '15 at 07:31
  • I highly recommend that you create a new question for this, otherwise it is going to confuse other users seeing two different questions on the same page. Splitting up the questions will benefit other SO users. – Jason Evans Apr 22 '15 at 07:49
  • Jason i created a new question.Please check.Thnaks – user1638889 Apr 22 '15 at 08:23