views:

84

answers:

2

I am using ASP.NEt MVC for one of my project. In this I have Employee controller which can be called by www.Mysite.com/Employee/ url.

Also I have used JqGrid which uses followng to fetch data

url: "GetGridData"

While testing the same I found that

  1. If i type www.Mysite.com/Employee/ in browser a call is made to

    www.Mysite.com/Employee/GetGridData

  2. If i type www.Mysite.com/Employee in browser a call is made to

    www.Mysite.com/GetGridData

Note: the missing / at the end in second call.

How to rectify this as the chances are end user can type any of this url in browser.

A: 

Try debugging your route: Phil Haack's: ASP.NET Routing Debugger

Gidon
+2  A: 

I'd take a look at how you're asking JqGrid to make it's web service call - because it won't know anything about MVC's routing engine by default - and this is all happening client side.

Stepping outside of MVC for a minute, if I have a page:

example.com/page1.aspx

And have a relative link to another page on there:

<a href="page2.aspx">Click here</a>

The browser will look for page2.aspx at the same level as page1.aspx, i.e.

example.com/page2.aspx

If I move page1 to a new folder:

example.com/NewFolder/page1.aspx

The browser will ask for

example.com/NewFolder/page2.aspx

when a user clicks on the link.

The same thing is happening to your GetGridData call - these are being made by the web browser to your server based on the information it has available to it.

So if your page responds on:

example.com/Employee

And asks for a relative request to:

GetGridData

The browser will send that request to the same level that Employee appears to be on:

example.com/GetGriddata

Which then fails because the routing engine can't find a route for that request.

You should look at generating the URL for the GetGridData call dynamically through the routing system, which will ensure that it's built as:

url: "/Employee/GetGridData"


Final edit to add

Forgot to mention, you should probably use the UrlHelper Action methods for this:

url: <%=Url.Action("GetGridData")%>

This will generate a path to the GetGridData method on the current controller. If you need to access a different controller, or pass some values, there are overloads to help.

Zhaph - Ben Duguid
Thanks for the reply but this would not serve the purpose as if I try using Employee/GetgridData now it generateswww.Mysite.com/Employee/GetGridData when called without / and www.Mysite.com/Employee/Employee/GetGridData when called with /
Sachin
Sorry, should have read: "/Employee/GetGridData"
Zhaph - Ben Duguid