0

I'm trying to use a Kendo Grid for a list of objects on my model, but the url's generated by the .Create() etc. methods are not generating the url correctly.

It doesn't appear to be just Kendo though because even in my controller using Url.Action() generates the wrong url.

// POST: Assessment/Create
[HttpPost]
[ValidateAntiForgeryToken]
[Route("eForms/Assessment/Create")] // <-- Tried with and without this
public ActionResult Create(AssessmentPoco model)
{
    var x = Url.Action(("Allergy_Read", "Assessment");
}

    //POST: Assessment/Allergy_Read
    [HttpPost, ActionName("Allergy_Read")]
    [Route("AllergyRead", Name = "Allergy_Read")]
    public ActionResult Allergy_Read([DataSourceRequest] DataSourceRequest request, AssessmentAllergiesSection model) //, int id)
    {
        return Json(new[] { model }.ToDataSourceResult(request, ModelState));
    }

Expected: eForms/Assessment/Allergy_Read

Actual: /?action=Allergy_Read&controller=Assessment

Route config:

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

        //web forms default
        routes.MapPageRoute(
            routeName: "WebFormDefault", 
            routeUrl: "", 
            physicalFile:"~/default.aspx");

        routes.MapRoute(
            name:  "API",
            url: "eforms/api/{controller}/{action}/{id}",
            defaults: new {controller="Customer", action="GetCustomers", id = UrlParameter.Optional}
            );

        ////mvc default
        routes.MapRoute(
            name: "Default",
            url: "eforms/{controller}/{action}/{id}",
            defaults: new { controller = "IncidentReports", action = "Search", id = UrlParameter.Optional }
        );
    }

Not sure what else could be at fault here (besides my brain), any ideas?

Clarifications (from comments):

  • We are using Areas
  • Global.asax is calling RegisterRoutes (also tried turning it off no change)

Update: This project is a newly added MVC project to an existing ASP.Net WebForms app. I updated the Route config because I was using looking at the wrong one.

2
  • Are you using areas? Commented Jul 29, 2015 at 22:42
  • In your global.asax, did you make sure that your RegisterRoutes is called? Commented Jul 29, 2015 at 22:59

2 Answers 2

1

You're using routeAttribute, so put a name inside it and use Html.RouteLink or Url.RouteUrl instead of Url.Action().

Example:

[Route("menu", Name = "mainmenu")]
public ActionResult MainMenu() { ... }

Usage in View:

<a href="@Url.RouteUrl("mainmenu")">Main menu</a>
Sign up to request clarification or add additional context in comments.

4 Comments

I get this error: A route named 'Allergy_Read' could not be found in the route collection. Parameter name: name
Put [RoutePrefix("Assessment")] just before the class declaration and try to see if you get any good results.
[RoutePrefix("Assessment")] public class AssessmentController : BaseFormController Same Error
This is not the answer I was hoping for, but it was the only way we could solve our problem. But to get this to work we needed to add "routes.MapMvcAttributeRoutes();" to the RegisterRoutes(). Not what we wanted but for now it's the only solution we have.
0

I tried this code in controller:

public class HomeController : Controller
   {
      [Route("AllergyRead", Name = "Allergy_Read")]   
      public ActionResult Allergy_Read()
      {
      return View();
      }
   }

And: @Html.RouteLink("Allergy Read", "Allergy_Read")

Give me the right route to action. I can't figure out why your implementation isn't working.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.