Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

My controller looks like this:

public ActionResult Index(string username)
        {
            if (string.IsNullOrEmpty(username))
            {
                _userId = User.Identity.GetUserId();
            }
            else
            {
                var user = UserService.GetUserByUserName(username);

                if (user != null)
                {
                    _userId = user.Id;
                }
                else
                {
                    return RedirectToAction("Index", "Routines");
                }
            }

            return View();
        }

        [HttpGet]
        public JsonResult GetUserHomeData()
        {
            return Json(CreateHomeViewModel(), JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public JsonResult GetUserStatisticsOverview()
        {
            return Json(CreateUserStatisticsOverviewViewModel(), JsonRequestBehavior.AllowGet);
        }

And I have problem with parameter username of ActionResult Index. I've monitored username variable and if I type url like this: www.test.com/profile/someUserName

Variable username is assigned these values: 1. someUserName 2. GetUserHomeData 3. GetUserStatisticsOverview

I call these Get methods from my javaScript file, why is this happening and how can I prevent this, i.e. catch only "someUsername"

EDIT

Here is my route config:

    routes.MapRoute("Profile", "profile/{userName}",
                    new { controller = "Profile", action = "Index", userName = UrlParameter.Optional }
                    );

routes.MapRoute("Default", "{controller}/{action}/{id}",
                new {controller = "Home", action = "Index", id = UrlParameter.Optional}

                );

EDIT 2

Here is how I access Get methods (I'm using Angular's $http)

getResult: function() {

            var input = $http.get("/Profile/GetUserHomeData");

            var deferred = $q.defer();

            deferred.resolve(input);

            return deferred.promise;
        }
share|improve this question
1  
What is your route-Table looking like? –  TGlatzer Jan 9 '14 at 16:11
    
@Grumbler85 I've edited post and added code for routes –  hyperN Jan 9 '14 at 16:12
1  
And how do you access the GetUserHomeData for example? –  TGlatzer Jan 9 '14 at 16:13
    
Please, specify the tested urls, and the resulting parameter, and controller action invoked. –  JotaBe Jan 9 '14 at 16:17
    
@Grumbler85 I've edited post again and added that piece of code –  hyperN Jan 9 '14 at 16:17

1 Answer 1

up vote 2 down vote accepted

Problem is you are probably calling something like: @Url.Action("GetUserHomeData", "Profile") inside your JS, but this will be catched by the first route and the action there is Index and nothing else. You can eliminate the problem by just removing the route

routes.MapRoute("Profile", "profile/{userName}",
                new { controller = "Profile", action = "Index", userName = UrlParameter.Optional }
                );

alternatively you can rewrite the rule (that it does not match the controller-name):

routes.MapRoute("ProfileShortRoute", "p/{userName}",
                new { controller = "Profile", action = "Index", userName = UrlParameter.Optional }
                );

This will lead to Urls like this: http://domain/Profile/?userName=someUser or http://domain/p/someUser

share|improve this answer
    
Is there any other way around this ? –  hyperN Jan 9 '14 at 16:21
1  
You can rewrite the route to not match the controller name –  TGlatzer Jan 9 '14 at 16:21
    
That helped, thanks ! –  hyperN Jan 9 '14 at 16:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.