I am trying to create another controller for my Ajax handler - so now I have an AppController (The site controller) and an AjaxController (the Ajax request handler).
The problem is, that when I access http://LocalHost:82/Ajax , I get The resource cannot be found. When I access http://LocalHost:82/Ajax/Index , it works.
The problem is the default routing, right? Here is my routing:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "App", action = "NewRequests", id = UrlParameter.Optional } // Parameter defaults
);
If you need more info dont hesitate to ask. Thanks!
Your routing:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "App", action = "NewRequests", id = UrlParameter.Optional } // Parameter defaults
);
Declares that default action is NewRequests, so it is expected that your AjaxController would have [HttpGet] NewRequests actions. You can do that by,
[HttpGet]
public ActionResult NewRequests()
{
// ...
}
or
[HttpGet, ActionName("NewRequests")]
public ActionResult WhatEverNameOfActionYouLike()
{
// ...
}
Is there an NewRequests method returning an ActionResult in the Ajax controller? If not, this makes sense as your default action is NewRequests.
Related
I am using
'routes.MapRoute'
to take one parameter without calling the action name. it works fine, but now my problem is whenever I wanted to use any other action form the same controller it's always calling the same action that is described on the 'routes.MapRoute'.
My ajax calling for all other action has GET type. but still, it's calling the same action that is described in the routes.MapRoute.
//This is my custom route.
routes.MapRoute(
"kuenstler",
"kuenstler/{name}",
new { controller = "kuenstler", action = "Index" }
);
//my default route
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Site", action = "Index", id
=
UrlParameter.Optional }
);
The first route will be called from a link attribute from another page so I need to get the name from there but without the action name.
link
action that needs to get the name from the calling.
public ActionResult Index(string name)
{
return View();
}
[HttpGet]
public ActionResult GetKuenstlerGalleryData(int? artistId, string
direction)
{
/// some code
}
Every time I am clicking the link
//localhost:50519/Kuenstler/firstname-lastname.
I am getting the name in my Index. Then I am calling the second action from javascript with GET type. But every time it's coming in the Index with the calling action name as parameter.
But every time it's coming in the Index with the calling action name as parameter.
Of course it does, your requests path perfectly fits the kuenstler route template.
You can map specific route for GetKuenstlerGalleryData action right before 'kuenstler' one. Ex.
routes.MapRoute(
"kuenstler-gallery-data",
"kuenstler/GetKuenstlerGalleryData",
new { controller = "kuenstler", action = "GetKuenstlerGalleryData" }
);
routes.MapRoute(
"kuenstler",
"kuenstler/{name}",
new { controller = "kuenstler", action = "Index" }
);
or use separate template, ex.
routes.MapRoute(
"kuenstler-gallery-data",
"kuenstler-gallery-data",
new { controller = "kuenstler", action = "GetKuenstlerGalleryData" }
);
or even use attribute routing
I was wondering if this is possible. Say I have a monolithic Controller, ReportController.cs.
I want to make a totally separate controller file but still keep the /Report/ in the url that we've some to know and expect.
What I tried was this in my global asax:
routes.MapRoute(
"Testing", // Route name
"{test}/{action}/{id}" // URL with parameters
);
and I added a new Controller named ReportTest.cs
the original route looks like this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "LandingPage", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Every time I try to call the simple action I have in ReportTest.cs I get this error: "Value cannot be null or empty. Parameter name: controllerName"
Am I misunderstanding how this works. When you have "{controller}/.." is this not saying 'look for any controllers named + controller and use that'. So if I go to .../Report/DoStuff it'll look for the method DoStuff on ReportController right?
So wouldn't my other route just append a search sequence? So if I put .../Report/DoStuff it'll look for the method DoStuff on ReportController and ReportTest right?
The routing format string:
{controller}/{action}/{id}
Means: the first part ("part" being "element after splitting on /") of the request URI is the controller name, the next part the action method and the last part the ID.
The placeholders {controller} and {action} are special. So your route {test}/{action}/{id} will not find any controller, as none is specified, and {test} doesn't mean anything. (Well it does, it'll get added as a route attribute named "test", and assigned a value representing that part of the request URI, so that is irrelevant for this scenario).
If you want to route an URI to a controller that is not mentioned in the URI, then you must literally specify the prefix, and the controller it should be routed to.
So your routing will look like this:
routes.MapRoute(
"SecondReportController",
"Report/NotOnReportController/{id}",
new { controller = "NotAReportController" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "LandingPage", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Then you can use the following controller:
public class NotAReportController : Controller
{
public ActionResult NotOnReportController(int id)
{
// ...
}
}
You can of course also use attribute routing instead:
public class NotAReportController : Controller
{
[Route("Report/NotOnReportController/{id}")]
public ActionResult NotOnReportController(int id)
{
// ...
}
}
So I have a controller that holds about 10 actions. Each action/view just simply holds an image. What I'm trying to do is have a parameter sent to the URL for each action (the parameter does the same thing for each action).
routes.MapRoute(
name: "MyRoute",
url: "MyController/{action}/{myparameter}",
defaults: new { controller = "MyController", action = "CanIMakeThisBeAnything", myparameter = UrlParameter.Optional }
);
Is there anyway I can make the action take in any action in the controller so I don't have to specify 10 different routes?
If I understand your comments correctly, you want to make the route look like:
routes.MapRoute("MyRoute", "{controller}/{action}/{myparameter}",
new
{
controller = "My",
action = "CanIMakeThisBeAnything",
myparameter = UrlParameter.Optional
});
And your action(s) like:
public ActionResult YesYouCanMakeThisAnything(string myparameter)
{
...
return View(...);
}
So if someone navigated to yourwebapp/My/YesYouCanMakeThisAnything/abc123, then it would call the above action and myparameter would equal "abc123".
If someone navigated to simply yourwebapp, then it would call the action named "CanIMakeThisBeAnything" (specified in your defaults for the route) on the controller "MyController" (also specified in your defaults for the route) and myparameter would equal null (because it has no default and is optional).
I recently upgraded from MVC 5 to MVC 3. I never registred a route, however, this URL worked:
http://www.testsite.com/shipworks/index/myemail#yahoo.com?website=testsite.com
Here is my code:
[HttpPost]
public ActionResult Index(string id, string website)
{
string data = string.Empty;
}
I now get a 404 with this code. I tried this route, however, it also fails:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"ShipWorks", // Route name
"{controller}/{action}/{id}/{website}", // URL with parameters
new { controller = "ShipWorks", action = "Index", email = UrlParameter.Optional, website =
UrlParameter.Optional }, new[] { "CloudCartConnector.Web.Controllers" }// Parameter defaults
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
What am I doing wrong? I put the shipping route above the default one. Note that my id is a string. The URL works fine with ?id=myemail#myemail.com.
First approach.
According to your path:
"{controller}/{action}/{id}/{website}"
you don't need to specify explicitly "website" property name. Then, you marked in your route that {id} and {website} are separated by slash /, so correct use of your route mask should be http://www.testsite.com/shipworks/index/myemail#yahoo.com/testsite.com.
However, here is one problem - dot symbol . will not be recognized correctly (in the way you want). So if you remove the dot, the path http://www.testsite.com/shipworks/index/myemail#yahoo.com/testsitecom will work.
Second approach.
In order to reach the results you want, you'd better pass e-mail and website as the query parameter, not as the part of the path.
You can use the following route config:
routes.MapRoute(
"ShipWorks", // Route name
"{controller}/{action}/{id}", // URL with parameters
new
{
controller = "ShipWorks",
action = "Index",
id = UrlParameter.Optional
}, new[] { "CloudCartConnector.Web.Controllers" }// Parameter defaults
);
Controller's action:
//[HttpPost]
public ActionResult Index(string email, string website)
{
(...)
return View();
}
And the query string: http://www.testsite.com/shipworks/index?email=myemail#yahoo.com&website=testsite.com.
Please note also, that since you marked your Index method with [HttpPost] attribute, you will get 404 using GET method (like typing in browser) even if you have correct URL.
I am trying to set up custom routing with the following mapped route
edit: my full route config
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
#region FixtureAdmin
routes.MapRoute(
name: "FixtureEdit",
url: "{controller}/{action}/{id}",
defaults: new { controller = "FixtureAdmin", action = "Edit", id = UrlParameter.Optional }
);
#endregion
#region Results
routes.MapRoute(
name: "ResultAdd",
url: "{controller}/{action}/{fixtureId}",
defaults: new { controller = "Result", action = "Add", fixtureId = UrlParameter.Optional }
);
#endregion
And my controller code
public ActionResult Add(int fixtureId)
{
// return model to view etc..
}
This is coming up with the exception, even though I have specified the parameter as optional.
The parameters dictionary contains a null entry for parameter 'fixtureId'
The strange thing is, if I change the parameter of the Add action to just 'Id' then the following URL will work Result/Add/1. I'm confused, is there some default routing that is overriding my custom one? Why would changing the parameter to just 'Id' work?
Edit
Just to test, I added another parameter to the action
public ActionResult Add(int? fixtureId, int? testId)
I then edited the route accordingly and now it works, so I reckon it is an issue with default routing.
Use a nullable int in your Controller Action.
public ActionResult Add(int? fixtureId)
{
// return model to view etc..
}
But the question is, if that is indeed an ID, how would it react if a null/blank ID is requested by the user? Is that ID a key in your DB? You can make it nullable if you are able to handle or provide a default page if the ID is blank/null.
EDIT:
This is because ASP.NET will assume that an unidentified parameter in your request is the id, in your case, Results/Add/1, 1 is unidentified. If you want to make that code work with using fixtureId, you should use Results/Add?fixureId=1. So ultimately, it's not because of the routing, but instead, it's because of the parameter in the Action that you have.
EDIT2:
Also, what you are experiencing there is called a routing conflict. Your routes are conflicting with the Default. You can try to apply constraints.
2,
from your post i think your problem is putting your custom route after default, like this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
name: "ResultAdd",
url: "{controller}/{action}/{fixtureId}",
defaults: new { controller = "Home", action = "Add", fixtureId = UrlParameter.Optional }
so:
1/ exception "The parameters dictionary contains a null entry for parameter 'fixtureId'" will come if you dont give the specific route name for any action link or route form because MVC will use default route to routing. So you need to give specific route name to your custom route can be worked, like this:
#Html.ActionLink("Test custom Route", "Add", "Home", new { fixtureId = 1 }, "ResultAdd")
Cheer
Look at this method of adding what the developer calls a 'NullableConstraint' clicky link So if the optional parameter is supplied you can do some checking on it's value.
And also look at the answer following the accepted answer for what seems a simpler, cleaner solution.