I got an error - No action was found on the controller 'Action' that matches the request.
The url is http://localhost:37331/api/action/FindByModule/1.
The routing I used is
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Controller:
public class ActionController : ApiController
{
private IActionRepository repository = null;
[HttpGet]
[ActionName("All")]
public IEnumerable<JsonAction> All()
{
return from action in this.repository.Get()
select new JsonAction
{
ID = action.ID,
Text = action.Text.Trim(),
Description = action.Description.Trim(),
};
}
[HttpGet]
[ActionName("FindByModule")]
public IEnumerable<JsonAction> FindByModule(Int64 moduleId)
{
return from action in this.repository.FindByModule(moduleId)
select new JsonAction
{
ID = action.ID,
Text = action.Text.Trim(),
Description = action.Description.Trim(),
};
}
}
This is because there is a parameter name mismatch. From your route the value 1 is assigned to parameter named id and your action is looking for parameter named moduleId.
First option is to change your route like this:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{moduleId}",
defaults: new { moduleId = RouteParameter.Optional }
);
Second is to change your URL like this:
http://localhost:37331/api/action/FindByModule?moduleId=1
So the parameter name match.
My api had too many parameters and I was getting an error. I solved the problem with Route.
[Route("addressverification/{id}/{no}/{day}/{month}/{year}")]
public AdressVerificationResult Get(long? id, long? no ,long? day, long? month, long? year)
{
return new AdressVerificationResult
{
Aciklama = "19........4 kimlik numaralı kişinin 18.......1 adres numarasında 'YerlesimYeri' adres tipi için geçerli bir yurtiçi adres beyanı mevcuttur.",
DurumKod = true
};
}
Related
I have configured routing like this:
config.Routes.MapHttpRoute(
name: "Sales",
routeTemplate: "api/{Sales}/{jsonresponce}",
defaults: new { controller = "Sales", action = "Postsomething" }
);
config.Routes.MapHttpRoute(
name: "User",
routeTemplate: "api/{User}/{GetDetails}",
defaults: new { controller = "User", action = "GetDetails" }
);
Here is my UserController:
public class UserController : ApiController
{
userservice objservice = new userservice();
[HttpGet]
public CustDetails GetDetails(string Username, string Password, string BillingFeedID)
{
CustDetails model = new CustDetails();
//checking for encrypted password
model.UserName = Username;
model.Password = Password;
model.BillingFeedID = BillingFeedID;
model = objservice.Login(model);
//taking merchant configuration data
var data = objservice.Getcustomerconfig(model.MerchantID, BillingFeedID);
model.LastPosBillID = data.LastPosBillID;
model.LastTimeStamp = data.LastTimeStamp;
model.SyncStatus = data.SyncStatus;
model.SynsTimeInterval = data.SynsTimeInterval;
model.DataSorce = data.DataSorce;
model.DataAuthentication = data.DataAuthentication;
model.DataBaseQuery = data.DataBaseQuery;
return model;
}
}
I also have a SalesController:
public class SalesController : ApiController
{
[HttpPost]
public async Task<HttpResponseMessage> PostSomething()
{
StringBuilder sb = new StringBuilder();
try
{
string jsonData = await Request.Content.ReadAsStringAsync();
// dynamic dataList = JArray.Parse(jsonData);
if (File.Exists(#"C:\MCarrots\Umairbills\Umairbills.json"))
File.Delete(#"C:\MCarrots\Umairbills\Umairbills.json");
File.Create(#"C:\MCarrots\Umairbills\Umairbills.json").Close();
File.WriteAllText(#"C:\MCarrots\Umairbills\Umairbills.json", jsonData);
return Request.CreateResponse(HttpStatusCode.OK, "OK");
}
catch (Exception ex)
{
File.WriteAllText(#"C:\MCarrots\mcarrots\Umairbills.json", ex.ToString());
return Request.CreateResponse(HttpStatusCode.NoContent, ex.ToString());
}
}
When I try to call the GetUserDetails action with this url:
http://localhost:42945/api/User/GetDetails?Username=kay001&Password=kay501&BillingFeedID=KF1
It is throwing this error:
"Message":"The requested resource does not support http method"
But the POST method in SalesController is working.
Your route templates seem off. I think they should be:
config.Routes.MapHttpRoute(
name: "Sales",
routeTemplate: "api/Sales/{action}",
defaults: new { controller = "Sales", action = "Postsomething" }
);
config.Routes.MapHttpRoute(
name: "User",
routeTemplate: "api/User/{action}",
defaults: new { controller = "User", action = "GetDetails" }
);
I changed the route templates so that the controller name is essentially hard-coded, and the action is a placeholder. The action can be left out in this case though, defaulting to GetDetails.
Hi. Is this possible to separate URL parameters for two placeholder {Name} and {Surname} like below ?
routes.MapRoute(
name: "Users",
url: "Authorization/{Name}.{Surname}",
defaults: new { controller = "Authorization", action = "Verify" }
);
And in my action method use following code :
private bool Verify (string Name,string Surname)
{
[...]
}
Or do I have to use one placeholder and parse my string to extract information :
routes.MapRoute(
name: "Users",
url: "Authorization/{UserName}",
defaults: new { controller = "Authorization", action = "Verify" }
);
And in Action method use following code :
private bool Verify(string UserName)
{
string name = "UserNameTillDot";
string surname = "UserNameAfterDot";
[...]
}
The first approach is totally fine.
The problem is that your action in controller is defined as private:
Instead of
private bool Verify (string Name, string Surname)
{
[...]
}
It should be
public ActionResult Verify (string Name,string Surname)
{
[...]
}
Also if you want to allow null for Name or Surname you should make them optional:
routes.MapRoute(
name: "Users",
url: "Authorization/{Name}-{Surname}",
defaults: new { controller = "Authorization", action = "Verify", Name = UrlParameter.Optional, Surname = UrlParameter.Optional }
);
You also should place this route before your default route.
EDIT:
There is a issue with "." in the route you can replace it with "-"
I have a simple Controller that is returning thumbnails, it is defined like:
public class ThumbnailsController : ApiController
{
public HttpResponseMessage Get(string id)
{
//code here
}
}
Everything works fine, I can access image using url http://site.com/api/Thumbnails/mylogin
But I would like to modify this method like so:
public class ThumbnailsController : ApiController
{
public HttpResponseMessage Get(string login="", int size=64)
{
//code here
}
}
Idea is to be able to call:
- http://site.com/api/Thumbnails/ - this will return current logged in user picture in default (64x64) size
- http://site.com/api/Thumbnails/mylogin - this will return mylogin user picture in default (64x64) size
- http://site.com/api/Thumbnails/mylogin/128 - this will return mylogin user picture in 128x128 size
My problem are routes, default route works with my unchanged method, but how should I change the default to get this working?
I will also have other Api Controllers but only this one should have custom route.
Here is my attempt, but it isn't working.
routes.MapHttpRoute(
name: "Thumbnails",
routeTemplate: "api/thumbnails/{login}/{size}",
defaults: new {controller="Thumbnails", action="Get", login = RouteParameter.Optional, size = RouteParameter.Optional }
);
EDIT
This is my Controller with test method:
public class ThumbnailsController : ApiController
{
public string Get(string login="", int size=64)
{
return string.Format("login: {0}, size: {1}", login, size);
}
}
and here is my RouteConfig:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional}
);
routes.MapHttpRoute(
name: "Thumbnails",
routeTemplate: "api/Thumbnails/{login}/{size}",
defaults: new { controller = "Thumbnails" , login = RouteParameter.Optional, size = RouteParameter.Optional }
);
}
}
Make sure you declare your custom route before the default route in the WebApiConfig.
EDIT:
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "Thumbnails",
routeTemplate: "api/Thumbnails/{login}/{size}",
defaults: new { controller = "Thumbnails", action = "Get",
login = RouteParameter.Optional, size = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
The custom route should be before the default one
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapHttpRoute(
name: "Thumbnails",
routeTemplate: "api/Thumbnails/{login}/{size}",
defaults: new { controller = "Thumbnails" , login = RouteParameter.Optional, size = RouteParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional}
);
}
And try to change the type of the "size" to nullable integer:
public HttpResponseMessage Get(string login = null, int? size = 64)
i'm getting this error when i'm navigate browser to url:
localhost:10793/RealEstates/10
this my RouteConfig code:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new { controller = "Main", action = "Index" }
);
routes.MapRoute(
name: "RealEstates",
url: "RealEstates/{action}",
defaults: new { controller = "RealEstates", action = "Index" }
);
routes.MapRoute(
name: "RealEstatesViewAd",
url: "RealEstates/{id}",
defaults: new { controller = "RealEstates", action = "ViewAd", id = UrlParameter.Optional }
);
}
}
my error:
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
when changed code to:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//routes.MapRoute(
// name: "Default",
// url: "{controller}/{action}",
// defaults: new { controller = "Main", action = "Index" }
//);
//routes.MapRoute(
// name: "RealEstates",
// url: "RealEstates/{action}",
// defaults: new { controller = "RealEstates", action = "Index" }
//);
routes.MapRoute(
name: "RealEstatesViewAd",
url: "RealEstates/{id}",
defaults: new { controller = "RealEstates", action = "ViewAd", id = UrlParameter.Optional }
);
}
}
it's work but when i call on other actions in controller
localhost:10793/RealEstates/CreateAd
this error found
The parameters dictionary contains a null entry for parameter 'id' of
non-nullable type 'System.Int32' for method
'System.Web.Mvc.ActionResult ViewAd(Int32)' in
'Youe3lan.Controllers.RealEstatesController'. An optional parameter
must be a reference type, a nullable type, or be declared as an
optional parameter.
Parameter name: parameters
and this my controller:
namespace MvcAppliction1.Controllers
{
public class RealEstatesController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult ViewAd(int id)
{
return View();
}
public ActionResult CreateAd()
{
return View();
}
}
}
You need to change it to:
routes.MapRoute(
name: "RealEstatesViewAd",
url: "RealEstates/{action}/{id}",
defaults: new { controller = "RealEstates", action = "ViewAd", id UrlParameter.Optional }
);}}
Have a look here it might help:
http://msdn.microsoft.com/en-us/library/cc668201(v=vs.100).aspx
UPDATE
Add this to your controller:
public ActionResult ViewAd(Int32 id)
{
return View();
}
You see
localhost:10793/RealEstates/10
is translated to:
localhost:10793/RealEstates/ViewAdd/10
So you need that method in the controller accepting an it parameter.
you've flagged your id in your route as optional:
id = UrlParameter.Optional
but I bet your controller isn't nullable??
public ActionResult ViewAd(Int32 id)
{
}
So you cant post a null into your id even though the route allows it. If you change this to:
public ActionResult ViewAd(Int32? id)
{
}
You won't get the error message:
The parameters dictionary contains a null entry for parameter 'id' of
non-nullable type 'System.Int32' for method
'System.Web.Mvc.ActionResult ViewAd(Int32)' in
'Youe3lan.Controllers.RealEstatesController'. An optional parameter
must be a reference type, a nullable type, or be declared as an
optional parameter. Parameter name: parameters
My case is very similar to this question, but since he did not get an answer I thought I'd throw some more input.
Everything works fine locally (on the VS embedded server). When I deploy to Azure, I get a 404 error accompanied by "No type was found that matches the controller named...".
However, when I load the routedebugger module the mapping seems ok even on Azure.
What can I do to debug that problem?
Thanks,
Alex
Edit: my routes are created this way:
GlobalConfiguration.Configuration.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
};
GlobalConfiguration.Configuration.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Edit 2: Here my controller class
public class EmployeeController : ApiController
{
// GET api/<controller>
public IEnumerable<Employee> Get()
{
using (var context = new nws())
{
return context.Employees;
}
}
// GET api/<controller>/5
public Employee Get(int id)
{
using (var context = new nws())
{
return context.Employees.FirstOrDefault(e => e.ID == id);
}
}
// GET api/<controller>/getbyatid/5
public Employee GetByAtId(string id)
{
using (var context = new nws())
{
return context.Employees.FirstOrDefault(e => e.AtUserID == id);
}
}
// POST api/<controller>
public void Post([FromBody]string value)
{
}
// PUT api/<controller>/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/<controller>/5
public void Delete(int id)
{
}
// GET api/<controller>/timebank/5
public int? GetTimeBank(string id)
{
using (var context = new nws())
{
var employee = context.Employees.FirstOrDefault(e => e.AtUserID == id);
if (employee != null)
return employee.GetTimeBank();
return null;
}
}
}
Switch the order of routes and try again.
GlobalConfiguration.Configuration.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
GlobalConfiguration.Configuration.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
};