I've been struggling with this can can't make any headway why no matter how close I follow the documentation or get help writing a controller, it simply responds with 404.
I saw this post here, but it doesn't make sense why it works here but not for me.
I'm using a namespace, its a brand new Umbraco project, everything compiles just fine, I've searched all over to find out why controllers respond with 404 and hit a wall.
Here's my controller. Somehow, I have only the first method working; GetMediaId. None of the other methods after it work. I can't find any real difference why they wouldn't work. I'm not even sure why the first method works to begin with when a simpler method doesn't work at all.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using dansandler_com4.Models;
using Umbraco.Web;
using Umbraco.Web.WebApi;
public class MediaApiController : UmbracoApiController
{
[HttpGet]
public MediaApiModel GetMediaById(int id)
{
var media = Umbraco.TypedMedia(id);
return new MediaApiModel
{
MediaId = media.Id,
MediaUrl = media.Url
// etc until all properties are set
};
}
[HttpGet]
public MediaApiModel GetMediaTest(int mediaTypeAlias) {
return new MediaApiModel { MediaId = 000, MediaUrl = "monkey" };
}
}
public class MediaFileModel
{
public int MediaId { get; set; }
public string MediaUrl { get; set; }
}
What updates are being made to a brand new Umbraco project, with a controller is made to make it work? This seems like it should be a straight forward affair but yet it's 404 only.
I added my usings to my code above. This is what is inconsistent. The first method in the controller works. Any other one, even if I do a hard coded return doesn't.
This for example:
public MediaApiModel GetMediaTest(int mediaTypeAlias) {
return new MediaApiModel { MediaId = 000, MediaUrl = "monkey" };
}
So
/umbraco/api/MediaApi/GetMediaTest/?mediaTypeAlias=1234 returns a 404?
I'd recommend you put [System.Web.Http.HttpGet] on the GetMediaTest and GetMediaById methods to make sure it's listening for the correct type of HttpGet.
Hard coding the using above each method doesn't change the result.
[System.Web.Http.HttpGet]
The target framework is 4.5, I'm running umbraco 7.2.8, everything in the web config is default and matches everything I can find online in regards to an impediment for a controller.
I'm using VS 2012 Ultimate, I tried newer VS express versions and saw the same issue. There has to be something other people are doing that is necessary to make this work.
The VS version should not matter at all, it just compiles to the same dll.
Your exact code above works for me when going to /umbraco/api/MediaApi/GetMediaTest/?mediaTypeAlias=1234 so there must be something strange going on in your solution. I am out of suggestions though.. No idea what that could be I'm afraid.
Thanks for the sample project. So what I noticed in this zip file is that it appears that when the controllers are created, they are somehow "registered" with the application. I'm saying registered since I don't know how else to explain it. They are identified in some way where they can be accessed later.
Simply taking your sample, adding a new method to the controller, rebuilding, it comes up 404. The previous methods still work but the new one is unknown. Between your project and mine, I'm not seeing any major differences in the web.config or package.config files that would make this not work for me. I don't know where else to look I'm spot checking around and I'm not seeing anything.
So I guess where to go from here is when a controller is created, where do its methods get "registered" with the application? How does it know that this controller should be accessible? Is there a way to edit the list of methods the controller has accessible? Like a config file or dll associated with the controller? I really don't know since whatever it is, it seems to be created automatically for everyone else but in my project(s) it is not.
Umbraco takes care of that.. And it works by writing : UmbracoApiController
So you're saying that you added a file in the controllers folder in my sample project with a class that inherits from UmbracoApiController and you can not use it?
Are you sure you don't have any code in your site that alters the routing table?
I'm not doing anything to change the default routing.
So just working in the sample project I added a new controller into the controllers folder.
using System.Web.Http;
using Umbraco.Web.WebApi;
namespace WebApplication30.Controllers
{
public class MediaGetterController : UmbracoApiController
{
[HttpGet]
public MediaApiModel GetMediaTest2(int id)
{
return new MediaApiModel { MediaId = 000, MediaUrl = "monkey" };
}
public class MediaApiModel
{
public int MediaId;
public string MediaUrl;
}
}
}
This returns a 404.
Going back to the original controller "MediaApiController" I changed an argument in the "GetMediaTest" method from "mediaTypeAlias" to "id". This doesn't update when I rebuild and run the site. It only works if I use the old argument in the query string which doesn't even appear in the controller.
using System.Web.Http;
using Umbraco.Web.WebApi;
namespace WebApplication30.Controllers
{
public class MediaApiController : UmbracoApiController
{
[HttpGet]
public MediaApiModel GetMediaById(int id)
{
var media = Umbraco.TypedMedia(id);
return new MediaApiModel
{
MediaId = media.Id,
MediaUrl = media.Url
};
}
[HttpGet]
public MediaApiModel GetMediaTest(int id)
{
return new MediaApiModel { MediaId = 000, MediaUrl = "monkey" };
}
public class MediaApiModel
{
public int MediaId;
public string MediaUrl;
}
}
}
I did notice that the sample project has a dll of the same name as the project. None of my other attempts create that. If I delete that dll from the bin folder, it does not get recreated and the original controller ceases operation.
Okay, it seems like your Visual Studio does not build the project for some reason. If the dll you deleted does not re-appear that means that visual studio is not actually building your project. So first of all, check the build output window in visual studio and make sure it's not showing any errors, if there are errors you need to fix your code.
If that's not it then there's not much I can help you with there, you need to maybe reset or reinstall visual studio to get that to work again.
Umbraco Controllers All Respond with 404
I've been struggling with this can can't make any headway why no matter how close I follow the documentation or get help writing a controller, it simply responds with 404.
I saw this post here, but it doesn't make sense why it works here but not for me.
https://our.umbraco.org/forum/developers/razor/63485-404-error-when-using-WebApi
I'm using a namespace, its a brand new Umbraco project, everything compiles just fine, I've searched all over to find out why controllers respond with 404 and hit a wall.
Here's my controller. Somehow, I have only the first method working; GetMediaId. None of the other methods after it work. I can't find any real difference why they wouldn't work. I'm not even sure why the first method works to begin with when a simpler method doesn't work at all.
What updates are being made to a brand new Umbraco project, with a controller is made to make it work? This seems like it should be a straight forward affair but yet it's 404 only.
I can't see your
usings
so please make sure that you're using[System.Web.Http.HttpGet]
as opposed toSystem.Web.Mvc.HttpGet
.When you do that this URL should work:
/umbraco/api/MediaApi/GetMediaById/?id=2303
I added my usings to my code above. This is what is inconsistent. The first method in the controller works. Any other one, even if I do a hard coded return doesn't.
This for example:
Should return something but comes up as 404.
So
/umbraco/api/MediaApi/GetMediaTest/?mediaTypeAlias=1234
returns a 404?I'd recommend you put
[System.Web.Http.HttpGet]
on theGetMediaTest
andGetMediaById
methods to make sure it's listening for the correct type ofHttpGet
.Correct.
This returns 404
so does this
Hard coding the using above each method doesn't change the result.
The target framework is 4.5, I'm running umbraco 7.2.8, everything in the web config is default and matches everything I can find online in regards to an impediment for a controller.
I'm using VS 2012 Ultimate, I tried newer VS express versions and saw the same issue. There has to be something other people are doing that is necessary to make this work.
The VS version should not matter at all, it just compiles to the same dll.
Your exact code above works for me when going to
/umbraco/api/MediaApi/GetMediaTest/?mediaTypeAlias=1234
so there must be something strange going on in your solution. I am out of suggestions though.. No idea what that could be I'm afraid.Here's my test solution, maybe you can find something that's different from yours: https://dl.dropboxusercontent.com/u/3006713/WebApplication30.zip
Hey Sebastiaan,
Thanks for the sample project. So what I noticed in this zip file is that it appears that when the controllers are created, they are somehow "registered" with the application. I'm saying registered since I don't know how else to explain it. They are identified in some way where they can be accessed later.
Simply taking your sample, adding a new method to the controller, rebuilding, it comes up 404. The previous methods still work but the new one is unknown. Between your project and mine, I'm not seeing any major differences in the web.config or package.config files that would make this not work for me. I don't know where else to look I'm spot checking around and I'm not seeing anything.
So I guess where to go from here is when a controller is created, where do its methods get "registered" with the application? How does it know that this controller should be accessible? Is there a way to edit the list of methods the controller has accessible? Like a config file or dll associated with the controller? I really don't know since whatever it is, it seems to be created automatically for everyone else but in my project(s) it is not.
Umbraco takes care of that.. And it works by writing
: UmbracoApiController
So you're saying that you added a file in the controllers folder in my sample project with a class that inherits from
UmbracoApiController
and you can not use it?Are you sure you don't have any code in your site that alters the routing table?
Also, can you list all the content of the file you added to the project here please?
I'm not doing anything to change the default routing.
So just working in the sample project I added a new controller into the controllers folder.
This returns a 404.
Going back to the original controller "MediaApiController" I changed an argument in the "GetMediaTest" method from "mediaTypeAlias" to "id". This doesn't update when I rebuild and run the site. It only works if I use the old argument in the query string which doesn't even appear in the controller.
I did notice that the sample project has a dll of the same name as the project. None of my other attempts create that. If I delete that dll from the bin folder, it does not get recreated and the original controller ceases operation.
Okay, it seems like your Visual Studio does not build the project for some reason. If the dll you deleted does not re-appear that means that visual studio is not actually building your project. So first of all, check the build output window in visual studio and make sure it's not showing any errors, if there are errors you need to fix your code.
If that's not it then there's not much I can help you with there, you need to maybe reset or reinstall visual studio to get that to work again.
Ok, I figured it out. Reinstalled VS, created a new application using the sample project, and its good ad gold now.
Thanks!
is working on a reply...