ASP.net MVC的ActionResult参数 [英] ASP.net mvc ActionResult parameter
问题描述
我的控制器看起来是这样的:
公众的ActionResult指数(用户名字符串)
{
如果(string.IsNullOrEmpty(用户名))
{
_userId = User.Identity.GetUserId();
}
其他
{
VAR用户= UserService.GetUserByUserName(用户名); 如果(用户!= NULL)
{
_userId = user.Id;
}
其他
{
返回RedirectToAction(指数,套路);
}
} 返回查看();
} [HTTPGET]
公共JsonResult GetUserHomeData()
{
返回JSON(CreateHomeViewModel(),JsonRequestBehavior.AllowGet);
} [HTTPGET]
公共JsonResult GetUserStatisticsOverview()
{
返回JSON(CreateUserStatisticsOverviewViewModel(),JsonRequestBehavior.AllowGet);
}
和我有一个ActionResult的指标参数的用户名的问题。我监视用户名变量,如果我输入的网址是这样的:www.test.com/profile/someUserName
变量名分配这些值:
1. someUserName
2. GetUserHomeData
3. GetUserStatisticsOverview
我把这些Get方法从我的JavaScript文件,为什么会出现这种情况,并哪能prevent这一点,即只抓someUsername
修改
下面是我的路线配置:
routes.MapRoute(档案,配置文件/ {用户名},
新{控制器=档案,行动=指数,用户名= UrlParameter.Optional}
);routes.MapRoute(默认,{控制器} / {行动} / {ID}
新{控制器=家,行动=索引,ID = UrlParameter.Optional} );
编辑2
下面是我如何访问Get方法(我用角的$ HTTP)
的getResult:功能(){ VAR输入= $ http.get(/资料/ GetUserHomeData); 变种推迟= $ q.defer(); deferred.resolve(输入); 返回deferred.promise;
}
但问题是你可能调用是这样的: @ Url.Action(GetUserHomeData,档案)
您的JS里面,但是这将是由第一路线,有首页
,没有别的动作捕获。
您可以通过只删除路径消除问题
routes.MapRoute(档案,配置文件/ {用户名},
新{控制器=档案,行动=指数,用户名= UrlParameter.Optional}
);
另外你可以重写规则(它不匹配的控制器名称):
routes.MapRoute(ProfileShortRoute,P / {用户名},
新{控制器=档案,行动=指数,用户名= UrlParameter.Optional}
);
这会导致这样的url:的http://域名/个人资料/用户名= SomeUser的
或的http://域名/ p / SomeUser的
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;
}
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
这篇关于ASP.net MVC的ActionResult参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!