404 not found after webapi project install of asp.net identity - asp.net

I added asp.net identity to my existing web project. now my controllers cannot be accessed. If I launch the local brower I am able to login in successfully. But when I go to one of the old controllers like
namespace PortwatchServiceV003.Controllers
{
[Authorize]
public class ValuesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
public string Get(int id)
{
return "value";
}
// POST api/values
public void Post([FromBody]string value)
{
}
// PUT api/values/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/values/5
public void Delete(int id)
{
}
}
}
it get error 404 not found.
Do the urls get somehow changed?
webapiconfig below...
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Services.Add(typeof(IExceptionLogger), new App_Start.AiExceptionLogger());
//config.EnableCors();
//var cors = new EnableCorsAttribute("http://localhost:57007", "*", "*");
//config.EnableCors(cors);
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
//use https ONLY
//config.Filters.Add(new UseSSLAttribute());
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}
}

Related

ApiController HttpPost parameters

The httpPost transfer with parameters fails in the apiconroller.
It is trying to communicate from Android to Web server.
I succeeded in communicating with Get and Post, which had no parameters.
However, if parameter is added in Post transmission, it fails. I certainly think there is a problem with the Web server code.
The tutorial only contains information about the Model. I want to exchange strings.
Global.asax.cs
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
//....
}
WebApiConfig.cs
public class WebApiConfig
{
public const string UrlPrefix = "api";
public const string UrlPrefixRelative = "~/" + UrlPrefix;
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
var httpControllerRouteHandler = typeof(HttpControllerRouteHandler).GetField("_instance",
System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
if (httpControllerRouteHandler != null)
{
httpControllerRouteHandler.SetValue(null,
new Lazy<HttpControllerRouteHandler>(() => new SessionHttpControllerRouteHandler(), true));
}
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: UrlPrefix + "/{controller}/{action}/{sn}",
defaults: new { action = "Index", sn = RouteParameter.Optional }
);
}
public class SessionControllerHandler : HttpControllerHandler, IRequiresSessionState
{
public SessionControllerHandler(RouteData routeData) : base(routeData) { }
}
public class SessionHttpControllerRouteHandler : HttpControllerRouteHandler
{
protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
=> new SessionControllerHandler(requestContext.RouteData);
}
}
ApiController.cs
public class LicenseController : ApiController
{
[HttpPost]
public HttpResponseMessage GetLicense([FromBody]string data)
{
return Request.CreateResponse(HttpStatusCode.OK, data);
}
[HttpGet]
public HttpResponseMessage GetLicense2(string data)
{
string udid = data;
string license = AES.Encrypt(udid);
return Request.CreateResponse(HttpStatusCode.OK, license);
}
[HttpPost]
public HttpResponseMessage GetLicense3()
{
return Request.CreateResponse(HttpStatusCode.OK, "ABC");
}
}
android code
new Thread(new Runnable() {
#Override
public void run() {
try{
// Defined URL where to send data
URL url = new URL("http://192.1.1.1:80/api/License/GetLicense/");
// Send POST data request
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
//wr.write(URLEncoder.encode("data=3434", "UTF-8") );
wr.write("data=3434");
wr.flush();
// Get the server response
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
{
// Append server response in string
sb.append(line + "\n");
}
}
catch(Exception ex)
{
}
}
}).start();
For a web api POST method accepting a single string parameter you can do:
[HttpPost]
public HttpResponseMessage GetLicense([FromBody]string data)
And then post the data from client like:
wr.write("=3434");
For multiple post parameters, create a model class in Web API:
public class DataModel {
public string data1 {get;set;}
public string data2 {get;set;}
}
Update api endpoint parameter type:
[HttpPost]
public HttpResponseMessage GetLicense([FromBody]DataModel dataModel)
Then post json string from client with content-type: "application/json"
{
"data1": "Data1 contents",
"data2": "Data2 contents"
}

How to configure route in Asp.Net WebApi to use action method name as a query parameter?

I want to invoke diffrent action methods based on the query string parameter, for example, webapi/mycontroller?action=getuser&id=10 should invoke mycontroller.getuser(10) action method and webapi/mycontroller?action=getallusers should invoke mycontroller.getallusers() action method. I tried to write the routing in the following way:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "webapi/{controller}?action={action}"
);
But this is not allowed, visual studio gives me the error The route URL cannot start with a '/' or '~' character and it cannot contain a '?' character.
So I've knocked something together that might help you get started
First create a route with custom handler
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}",
defaults: null,
constraints: null,
handler: new CustomHttpControllerDispatcher(config)
);
public class CustomHttpControllerDispatcher : HttpMessageHandler
{
private IHttpControllerSelector _controllerSelector;
private readonly HttpConfiguration _configuration;
public CustomHttpControllerDispatcher(HttpConfiguration configuration)
{
_configuration = configuration;
}
public HttpConfiguration Configuration
{
get { return _configuration; }
}
private IHttpControllerSelector ControllerSelector
{
get
{
if (_controllerSelector == null)
{
_controllerSelector = _configuration.Services.GetHttpControllerSelector();
}
return _controllerSelector;
}
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return SendAsyncInternal(request, cancellationToken);
}
private Task<HttpResponseMessage> SendAsyncInternal(HttpRequestMessage request, CancellationToken cancellationToken)
{
IHttpRouteData routeData = request.GetRouteData();
Contract.Assert(routeData != null);
HttpControllerDescriptor httpControllerDescriptor = ControllerSelector.SelectController(request);
IHttpController httpController = httpControllerDescriptor.CreateController(request);
foreach (var queryParam in request.GetQueryNameValuePairs())
{
routeData.Values.Add(queryParam.Key, queryParam.Value);
}
// Create context
HttpControllerContext controllerContext = new HttpControllerContext(_configuration, routeData, request);
controllerContext.Controller = httpController;
controllerContext.ControllerDescriptor = httpControllerDescriptor;
return httpController.ExecuteAsync(controllerContext, cancellationToken);
}
}
Then set your methods to get in the controller
public class MyController : ApiController
{
[HttpGet]
public IHttpActionResult GetUser([FromUri]int userId)
{
return Ok();
}
[HttpGet]
public IHttpActionResult DoSomething([FromUri]string test)
{
return Ok();
}
}
I've only tried with GET methods, POSTs may just work, but I haven't tested.

Web API and Mobile API with Bearer-Token

We are building Server for Web Api and Mobile Api using Bearer-Token with OWIN.
The requirements to validate are different between these two.
This is the Owin Configuration:
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
ConfigureOAuth(app);
}
public void ConfigureOAuth(IAppBuilder app)
{
OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
TokenEndpointPath = new PathString("/Api/Token"),
Provider = new ApplicationOAuthProvider(),
// RefreshTokenProvider = new ApplicationRefreshTokenProvider(),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(8),
AllowInsecureHttp = true,
};
// Enable the application to use bearer tokens to authenticate users
app.UseOAuthBearerTokens(OAuthServerOptions);
OAuthAuthorizationServerOptions mobOAuthServerOptions = new OAuthAuthorizationServerOptions()
{
TokenEndpointPath = new PathString("/Mobile/Token"),
Provider = new MobileOAuthProvider(),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(20),
AllowInsecureHttp = true,
};
// Enable the application to use bearer tokens to authenticate users
app.UseOAuthBearerTokens(mobOAuthServerOptions);
}
This is the routing:
public class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "Api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "MobileApi",
routeTemplate: "Mobile/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// Ignore any authentication that happens before the request reaches the Web Api pipeline, either by IIS or OWIN middleware.
// That way to restrict Web Api to authenticate only using bearer tokens.
config.SuppressDefaultHostAuthentication();
// enable authentication using bearer tokens.
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// configuration.Filters.Add(new TokenValidationAttribute());
}
}
Controller for API:
[Authorize]
public class EmployeeLeaveBalanceController : ApiBaseController
{
public EmployeeLeaveBalanceController()
: base()
{ }
[System.Web.Http.HttpGet]
public ApiResult GetLeaveBalance([FromUri] string payrollName, [FromUri] string employeeNumber)
{
try
{
SetContext(null);
ApiResult result = ValidateParameter(payrollName);
if (result != null)
return result;
EmployeeLeaveBalanceModel model = new EmployeeLeaveBalanceModel();
return model.GetLeaveBalance(payrollName, employeeNumber);
}
catch //(Exception)
{
return new ApiResult().SetMessage(ApiResultMessage.UnexceptedError);
}
}
private ApiResult ValidateParameter(string payrollName)
{
bool isValid = UploadHelper.GetPayroll(payrollName) != null;
return (!isValid)
? new ApiResult().SetMessage(ApiResultMessage.InvalidParameter)
: null;
}
}
For Mobile Api
[Authorize]
public class UserController : MobileBaseController
{
#region Get
[System.Web.Http.HttpGet]
public UserMdo Get()
{
try
{
SetContext();
UserModel model = new UserModel();
return model.GetUser(ApplicationContext.UserId);
}
catch(Exception)
{
throw;
// return new MobileResult().SetError(1000, ex.Message);
}
}
#endregion
}
However, I always get Unauthorized access when accessing Mobile Controller.
And Api Controller stopped working as well.
Did I miss anything? How do the controllers know that the bearer-token belongs to Web Api or Mobile Api? I think this is the problem.
This code to get the token and use it for authentication as well

Unable to reach ASP.NET MVC Web Api endpoint

I am working on an ASP.NET MVC app. I am trying to create a basic API. I created my first Web API controller by right-clicking on Controllers, Add -> Controller... then choosing "Web API 2 Controller - Empty". In the controller code, I have the following:
namespace MyProject.Controllers
{
public class MyApiController : ApiController
{
public IHttpActionResult Get()
{
var results = new[]
{
new { ResultId = 1, ResultName = "Bill" },
new { ResultId = 2, ResultName = "Ted" }
};
return Ok(results);
}
}
}
When I run the app, I enter http://localhost:61549/api/myApi in the browser's address bar. Unfortunately, I get a 404. I'm just trying to create an API endpoint that returns a hard-coded set of JSON objects. I need this to test some client-side JavaScript. What am I doing wrong?
Here are how my routes are registered:
WebApiConfig.cs
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
RouteConfig.cs
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 }
);
}
Make sure that you have the WebApiConfig registration being called, possibly in the Global.asax Application_Start() method. Something like:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
You did not add method name at the end of call. Try this one:
http://localhost:61549/api/myapi/get
Try this approach
namespace MyProject.Controllers
{
public class MyApiController : ApiController
{
public IHttpActionResult Get()
{
var results = new List<ResultModel>
{
new ResultModel() {ResultId = 1, ResultName = "Bill"},
new ResultModel() {ResultId = 2, ResultName = "Ted"}
};
return Ok(results);
}
}
public class ResultModel
{
public int ResultId { get; set; }
public string ResultName { get; set; }
}
}
Api: http://localhost:61549/api/MyApi/get
Hope this helps.

Web API Lowercase Route

Hy there,
I need to enforce Lowercase routes in my Web API project.
If it was an MVC project i would use something like
routes.LowercaseUrls = true;
But in Web API that property does not exists.
I tried the LowercaseRoutesMVC4 NuGet extension but my routes needs to have a custom handler so that extension does not help me.
What can I do?
This looks like it does what you need
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional },
constraints: new { url = new LowercaseRouteConstraint() }
);
}
}
public class LowercaseRouteConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
var path = httpContext.Request.Url.AbsolutePath;
return path.Equals(path.ToLowerInvariant(), StringComparison.InvariantCulture);
}
}
I found this at https://gist.github.com/benfoster/3274578#file-gistfile1-cs-L4
I have json config like
public class JsonConfig
{
public static void Initiliaze(HttpConfiguration config, bool isCamelCase)
{
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.None;
json.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
json.SerializerSettings.Formatting = Formatting.None;
if (isCamelCase)
json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.Formatters.Remove(config.Formatters.XmlFormatter);
}
}
You can call this method in WebApiConfig.cs like
JsonConfig.Initiliaze(config, true);

Resources