开发者

ASP.Net MVC redirecttoaction not passing action name in url

I have a simple create action to receive post form data, save to db and redirect to list view. The problem is, after redirecttoaction result excutes, the url on my browser lost the action section. Which it should be "http://{hotsname}/Product/List" but comes out as "http://{hotsname}/Product/". Below is my code:

    [HttpPost]
    public ActionResult Create(VEmployee model, FormCollection fc)
    {
        var facility = FacilityFactory.GetEmployeeFacility();

        var 开发者_如何转开发avatar = Request.Files["Avatar"].InputStream;

        var newModel = facility.Save(model, avatar);

        return RedirectToAction("List");
    }

The page can correctly render list view content, but since some links in this view page use relative url, the functions are interrupted. I am now using return Redirect("/Employee/List") to force the url. But I just wonder why the action name is missing. I use MVC3 and .Net framwork 4.

I am new to ASP.Net MVC, thanks for help.


Your route table definitely says that "List" action is default, so when you redirect to it as RedirectToAction("List") - routing ommits the action because it is default.

Now if you remove the default value from your routes - RedirectToAction will produce a correct (for your case) Url, but you'll have to double check elsewhere that you are not relying on List being a default action.


Well, Chris,

If you get the right content on http://{hotsname}/Product/ then it seems that routing make that URL point to List either indirectly (using pattern like {controller}/{action}) and something wrong happens when resolving URL from route or {action} parameter is just set wth default value List. Both URLs can point to the same action but the routing engine somehow takes the route without explicit action name.

You should check:

  • Order in which you define your routes
  • How many routes can possibly lead to EmployeeController.List()
  • Which one of those routes has the most priority
  • Default values for your routes

Just make the route with explicit values: employee/list to point to your List action and make sure that is the route to select when generating links (it should be most specific route if possible).

It would be nice if you provide your routes mappings here.

but since some links in this view page use relative url, the functions are interrupted.

Why do you make it that way? Why not generate all the links through routing engine?


When using the overload RedirectToAction("Action") you need to be specifying an action that is in the same controller. Since you are calling an action in a different controller, you need to specify the action with the alternate overload e.g. RedirectToAction("List", "Employee").

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜