ASP.Net HelpPages not showing my controllers - c#

I have been here before, but this time I have managed to get further than I have before.
I have been following this guide and done everything it has said.
If I navigate to http://localhost:61589/help I actually see the help page, but there is only the introduction, there are no descriptions.
In my controller I have comments (always do) like this:
/// <summary>
/// For all answer related endpoints
/// </summary>
[RoutePrefix("answers")]
public class AnswersController : ApiController
{
// Readonly properties
private readonly IUnitOfWork _unitOfWork;
private readonly AnswerService _service;
private readonly StateService _stateService;
/// <summary>
/// Default constructor
/// </summary>
public AnswersController()
{
// Map our properties
this._unitOfWork = new UnitOfWork<DatabaseContext>();
this._service = new AnswerService(this._unitOfWork);
this._stateService = new StateService(this._unitOfWork);
}
/// <summary>
/// Get a list of answers
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("")]
public async Task<IHttpActionResult> GetAllAsync()
{
try
{
// Return all our answers
return Ok(await this._service.GetAllAsync("States.Filters"));
// If there is an error
}
catch (Exception ex)
{
// Return our error
return BadRequest(ex.Message.ToString());
}
}
/// <summary>
/// Get a answer by id
/// </summary>
/// <param name="id">The answer id</param>
/// <returns></returns>
[HttpGet]
[Route("")]
public async Task<IHttpActionResult> GetAsync(int id)
{
try
{
// Return all our answers
return Ok(await this._service.GetAsync(id, "States.Filters"));
// If there is an error
}
catch (Exception ex)
{
// Return our error
return BadRequest(ex.Message.ToString());
}
}
/// <summary>
/// Create a answer
/// </summary>
/// <param name="model">The answer model</param>
/// <returns></returns>
[HttpPost]
[Route("")]
public async Task<IHttpActionResult> CreateAsync(Answer model)
{
try
{
// Get our states
var all = await this._stateService.GetAllAsync("Filters");
var states = all.Where(m => model.States.Any(s => s.Id == m.Id)).ToList();
// Create our model
var answer = new Answer
{
Text = model.Text,
QuestionId = model.QuestionId,
Order = model.Order,
States = states
};
// Save our model
this._service.Create(answer);
// Save the database changes
await this._unitOfWork.SaveChangesAsync();
// Return our updated model
return Ok(answer);
// If there is an error
}
catch (Exception ex)
{
// Return our error
return BadRequest(ex.Message.ToString());
}
}
/// <summary>
/// Update a answer
/// </summary>
/// <param name="model">The answer model</param>
/// <returns></returns>
[HttpPut]
[Route("")]
public async Task<IHttpActionResult> UpdateAsync(Answer model)
{
try
{
// Create our model
var answer = new Answer
{
Id = model.Id,
QuestionId = model.QuestionId,
Order = model.Order,
Text = model.Text
};
// Save our model
this._service.Update(answer);
// Save the database changes
await this._unitOfWork.SaveChangesAsync();
// Return our updated model
return Ok(model);
// If there is an error
}
catch (Exception ex)
{
// Return our error
return BadRequest(ex.Message.ToString());
}
}
/// <summary>
/// Delete a answer
/// </summary>
/// <param name="id">The answer id</param>
/// <returns></returns>
[HttpDelete]
[Route("")]
public async Task<IHttpActionResult> DeleteAsync(int id)
{
try
{
// Get our model
var model = await this._service.GetAsync(id);
// Save our model
this._service.Remove(model);
// Save the database changes
await this._unitOfWork.SaveChangesAsync();
// Return Ok
return Ok();
// If there is an error
}
catch (Exception ex)
{
// Return our error
return BadRequest(ex.Message.ToString());
}
}
}
In the generated XmlDocument.xml I have these memebers:
<member name="T:Piiick.Api.Controllers.AnswersController">
<summary>
For all answer related endpoints
</summary>
</member>
<member name="M:Piiick.Api.Controllers.AnswersController.#ctor">
<summary>
Default constructor
</summary>
</member>
<member name="M:Piiick.Api.Controllers.AnswersController.GetAllAsync">
<summary>
Get a list of answers
</summary>
<returns></returns>
</member>
<member name="M:Piiick.Api.Controllers.AnswersController.GetAsync(System.Int32)">
<summary>
Get a answer by id
</summary>
<param name="id">The answer id</param>
<returns></returns>
</member>
<member name="M:Piiick.Api.Controllers.AnswersController.CreateAsync(Piiick.Data.Models.Answer)">
<summary>
Create a answer
</summary>
<param name="model">The answer model</param>
<returns></returns>
</member>
<member name="M:Piiick.Api.Controllers.AnswersController.UpdateAsync(Piiick.Data.Models.Answer)">
<summary>
Update a answer
</summary>
<param name="model">The answer model</param>
<returns></returns>
</member>
<member name="M:Piiick.Api.Controllers.AnswersController.DeleteAsync(System.Int32)">
<summary>
Delete a answer
</summary>
<param name="id">The answer id</param>
<returns></returns>
</member>
But they are not appearing on the actual help page.
Does anyone have any idea why this might be happening?

Turns out I only had to add this line:
GlobalConfiguration.Configure(WebApiConfig.Register);
to my Startup.cs file in the Configuration method.
Once this was added, it started working.
For anyone else, that looks like this:
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
// Get our http configuration
var config = new HttpConfiguration();
// Register all areas
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
// Use our web api
app.UseWebApi(config);
}
}

According to http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/creating-api-help-pages
You can use XML documentation comments to create the documentation. To enable this feature, open the file Areas/HelpPage/App_Start/HelpPageConfig.cs and uncomment the following line:
config.SetDocumentationProvider(new XmlDocumentationProvider(
HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));

Related

Accessing Cookies from static class in ASP.net Core

I'm Trying to access Cookies from Static Class in Asp.net Core , So far I couldn't find any useful topics on Google. any Suggestions?
I already Did that in asp.net:
public static string GetString(string Key)
{
var format = System.Web.HttpContext.Current.Request.Cookies["Language"];
if (format == null)
format = new HttpCookie("Language") { Value = "Fa" };
ResourceManager rm = new ResourceManager("Ippv.Virtual.Application.Properties." + format.Value,
Assembly.GetExecutingAssembly());
return rm.GetString(Key);
}
How can I write equivalent of this code in asp.net core?
In ASP.NET, we can access cookies using httpcontext.current but in ASP.NET Core, there is no htttpcontext.currently. In ASP.NET Core, everything is decoupled and modular.Httpcontext is accessible from Request object and the IHttpContextAccessor interface which is under "Microsoft.AspNetCore.Http" namespace and this is available anywhere in the application. Refer to the following code:
public class TestController : Controller
{
private readonly IHttpContextAccessor _httpContextAccessor;
public TestController(IHttpContextAccessor httpContextAccessor)
{
this._httpContextAccessor = httpContextAccessor;
}
public IActionResult Index()
{
//read cookie from IHttpContextAccessor
string cookieValueFromContext = _httpContextAccessor.HttpContext.Request.Cookies["key"];
//read cookie from Request object
string cookieValueFromReq = Request.Cookies["Key"];
//set the key value in Cookie
Set("kay", "Hello from cookie", 10);
//Delete the cookie object
Remove("Key");
return View();
}
/// <summary>
/// Get the cookie
/// </summary>
/// <param name="key">Key </param>
/// <returns>string value</returns>
public string Get(string key)
{
return Request.Cookies[key];
}
/// <summary>
/// set the cookie
/// </summary>
/// <param name="key">key (unique indentifier)</param>
/// <param name="value">value to store in cookie object</param>
/// <param name="expireTime">expiration time</param>
public void Set(string key, string value, int? expireTime)
{
CookieOptions option = new CookieOptions();
if (expireTime.HasValue)
option.Expires = DateTime.Now.AddMinutes(expireTime.Value);
else
option.Expires = DateTime.Now.AddMilliseconds(10);
Response.Cookies.Append(key, value, option);
}
/// <summary>
/// Delete the key
/// </summary>
/// <param name="key">Key</param>
public void Remove(string key)
{
Response.Cookies.Delete(key);
}

How to make non-obligatory param in swagger for .net core api method

I noticed, that my swagger index page, renders a mandatory field id although I set in the code [HttpGet("{id?}")]
Here is the whole method:
/// <summary>
/// Returns all projects
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[ProducesDefaultResponseType]
[HttpGet("{id?}")]
public async Task<IActionResult> Get([FromRoute] int? id)
{
return Ok(await Mediator.Send(new GetProjectsQuery { Id = id }));
}
Where I went wrong?
Try this. Remove the id? from your HttpVerb and the FromRoute from method signature.
/// <summary>
/// Returns all projects
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[ProducesDefaultResponseType]
[HttpGet]
public async Task<IActionResult> Get(int? id)
{
return Ok(await Mediator.Send(new GetProjectsQuery { Id = id }));
}

Unit test custom IActionResult.ExecuteResultAsync(ActionContext) in ASP.NET Core 2.0

Having the below class
/// <summary>
/// An unsuccessful HTTP result.
/// </summary>
public class UnsuccessfulActionResult : IActionResult, IHttpStatusCodeResult, IErrors
{
/// <inheritdoc />
/// <summary>
/// The HTTP status code.
/// </summary>
public HttpStatusCode StatusCode { get; }
/// <summary>
/// The corresponding error messages, if any.
/// </summary>
public IReadOnlyCollection<string> Errors { get; }
/// <summary>
/// The request Transaction ID.
/// </summary>
public string TransactionId { get; }
/// <summary>
/// Default ctor.
/// </summary>
/// <param name="transactionId">The request Transaction ID.</param>
/// <param name="errors">The corresponding error messages, if any.</param>
/// <param name="httpStatusCode"></param>
public UnsuccessfulActionResult(
string transactionId,
HttpStatusCode httpStatusCode,
IReadOnlyCollection<string> errors = null)
{
TransactionId = transactionId;
StatusCode = httpStatusCode;
Errors = errors ?? new List<string>();
}
/// <summary>
/// Convenient method to create an unsuccessful response model.
/// </summary>
/// <param name="statusCode"></param>
/// <returns></returns>
protected UnsuccessfulResponseModel CreateResponseModel(int statusCode)
{
return new UnsuccessfulResponseModel(TransactionId, statusCode, Errors);
}
/// <inheritdoc />
public virtual Task ExecuteResultAsync(ActionContext context)
{
var objectResult = new ObjectResult(CreateResponseModel((int)StatusCode))
{
StatusCode = (int)StatusCode
};
return objectResult.ExecuteResultAsync(context);
}
}
I need to test this class to make sure that ActionContext has the response provided in overridden ExecuteResultAsync.
Just started going deep into ASP.NET Core 2.0 and missing some parts, in Web API 2 I would just check the HttpResponseMessage returned in the equivalent method of IHttpActionResult.
Task<HttpResponseMessage> ExecuteAsync(
CancellationToken cancellationToken
)

Application_Error and ExceptionFilter

So I have recently built at ExceptionFilter which handles all errors except Api Errors.
The ExceptionFilter looks like this:
public class ExceptionAttribute : IExceptionFilter
{
/// <summary>
/// Handles any exception
/// </summary>
/// <param name="filterContext">The current context</param>
public void OnException(ExceptionContext filterContext)
{
// If our exception has been handled, exit the function
if (filterContext.ExceptionHandled)
return;
// If our exception is not an ApiException
if (!(filterContext.Exception is ApiException))
{
// Set our base status code
var statusCode = (int)HttpStatusCode.InternalServerError;
// If our exception is an http exception
if (filterContext.Exception is HttpException)
{
// Cast our exception as an HttpException
var exception = (HttpException)filterContext.Exception;
// Get our real status code
statusCode = exception.GetHttpCode();
}
// Set our context result
var result = CreateActionResult(filterContext, statusCode);
// Set our handled property to true
filterContext.ExceptionHandled = true;
}
}
/// <summary>
/// Creats an action result from the status code
/// </summary>
/// <param name="filterContext">The current context</param>
/// <param name="statusCode">The status code of the error</param>
/// <returns></returns>
protected virtual ActionResult CreateActionResult(ExceptionContext filterContext, int statusCode)
{
// Create our context
var context = new ControllerContext(filterContext.RequestContext, filterContext.Controller);
var statusCodeName = ((HttpStatusCode)statusCode).ToString();
// Create our route
var controller = (string)filterContext.RouteData.Values["controller"];
var action = (string)filterContext.RouteData.Values["action"];
var model = new HandleErrorInfo(filterContext.Exception, controller, action);
// Create our result
var view = SelectFirstView(context, string.Format("~/Views/Error/{0}.cshtml", statusCodeName), "~/Views/Error/Index.cshtml", statusCodeName);
var result = new ViewResult { ViewName = view, ViewData = new ViewDataDictionary<HandleErrorInfo>(model) };
// Return our result
return result;
}
/// <summary>
/// Gets the first view name that matches the supplied names
/// </summary>
/// <param name="context">The current context</param>
/// <param name="viewNames">A list of view names</param>
/// <returns></returns>
protected string SelectFirstView(ControllerContext context, params string[] viewNames)
{
return viewNames.First(view => ViewExists(context, view));
}
/// <summary>
/// Checks to see if a view exists
/// </summary>
/// <param name="context">The current context</param>
/// <param name="name">The name of the view to check</param>
/// <returns></returns>
protected bool ViewExists(ControllerContext context, string name)
{
var result = ViewEngines.Engines.FindView(context, name, null);
return result.View != null;
}
}
As you can see, if the error is not an ApiException then I route the user to the error controller.
The ApiException is just an error that happens when I make an API call from within MVC.
When these errors happen I would like to return the error as JSON back to the client so that the JavaScript can handle it.
I thought not handling the error would do this, but instead it generates a server error (albeit with the JSON error in it) like so:
Server Error in '/' Application.
{"message":"validateMove validation failure:\r\nThe item is despatched and cannot be moved"}
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: SapphirePlus.Web.ApiException: {"message":"validateMove validation failure:\r\nThe item is despatched and cannot be moved"}
Source Error:
Line 181: if (response.StatusCode != HttpStatusCode.OK)
Line 182: throw new ApiException(result);
So my question is, can I get the Application_Error method to get errors that ARE ApiExceptions and return the error as JSON?
So my question is, can I get the Application_Error method to get
errors that ARE ApiExceptions and return the error as JSON?
Of course:
protected void Application_Error()
{
var apiException = Server.GetLastError() as ApiException;
if (apiException != null)
{
Response.Clear();
Server.ClearError();
Response.StatusCode = 400;
Context.Response.ContentType = "application/json";
Context.Response.Write("YOUR JSON HERE");
}
}
In the end I didn't need to use Global.asax at all, I was able to handle it all inside my ExceptionAttribute class like this:
public class ExceptionAttribute : IExceptionFilter
{
/// <summary>
/// Handles any exception
/// </summary>
/// <param name="filterContext">The current context</param>
public void OnException(ExceptionContext filterContext)
{
// If our exception has been handled, exit the function
if (filterContext.ExceptionHandled)
return;
// Set our base status code
var statusCode = (int)HttpStatusCode.BadRequest;
// If our exception is an http exception
if (filterContext.Exception is HttpException)
{
// Cast our exception as an HttpException
var exception = (HttpException)filterContext.Exception;
// Get our real status code
statusCode = exception.GetHttpCode();
}
// Set our context result
var result = CreateActionResult(filterContext, statusCode);
// Set our handled property to true
filterContext.Result = result;
filterContext.ExceptionHandled = true;
}
/// <summary>
/// Creats an action result from the status code
/// </summary>
/// <param name="filterContext">The current context</param>
/// <param name="statusCode">The status code of the error</param>
/// <returns></returns>
protected virtual ActionResult CreateActionResult(ExceptionContext filterContext, int statusCode)
{
// Create our context
var context = new ControllerContext(filterContext.RequestContext, filterContext.Controller);
var statusCodeName = ((HttpStatusCode)statusCode).ToString();
// Create our route
var controller = (string)filterContext.RouteData.Values["controller"];
var action = (string)filterContext.RouteData.Values["action"];
var model = new HandleErrorInfo(filterContext.Exception, controller, action);
// Create our result
var view = SelectFirstView(context, string.Format("~/Views/Error/{0}.cshtml", statusCodeName), "~/Views/Error/Index.cshtml", statusCodeName);
var result = new ViewResult { ViewName = view, ViewData = new ViewDataDictionary<IError>(this.Factorize(model)) };
// Return our result
return result;
}
/// <summary>
/// Factorizes the HandleErrorInfo
/// </summary>
/// <param name="error"></param>
/// <returns></returns>
protected virtual IError Factorize(HandleErrorInfo error)
{
// Get the error
var model = new Error
{
Message = "There was an unhandled exception."
};
// If we have an error
if (error != null)
{
// Get our exception
var exception = error.Exception;
// If we are dealing with an ApiException
if (exception is ApiException || exception is HttpException)
{
// Get our JSON
var json = JObject.Parse(exception.Message);
var message = json["exceptionMessage"] != null ? json["exceptionMessage"] : json["message"];
// If we have a message
if (message != null)
{
// Update our model message
model.Message = message.ToString();
}
}
else
{
// Update our message
model.Message = exception.Message;
}
}
// Return our response
return model;
}
/// <summary>
/// Gets the first view name that matches the supplied names
/// </summary>
/// <param name="context">The current context</param>
/// <param name="viewNames">A list of view names</param>
/// <returns></returns>
protected string SelectFirstView(ControllerContext context, params string[] viewNames)
{
return viewNames.First(view => ViewExists(context, view));
}
/// <summary>
/// Checks to see if a view exists
/// </summary>
/// <param name="context">The current context</param>
/// <param name="name">The name of the view to check</param>
/// <returns></returns>
protected bool ViewExists(ControllerContext context, string name)
{
var result = ViewEngines.Engines.FindView(context, name, null);
return result.View != null;
}
}
This handled any Mvc error and for my Api calls, I did this:
/// <summary>
/// Used to handle the api response
/// </summary>
/// <param name="response">The HttpResponseMessage</param>
/// <returns>Returns a string</returns>
private async Task<string> HandleResponse(HttpResponseMessage response)
{
// Read our response content
var result = await response.Content.ReadAsStringAsync();
// If there was an error, throw an HttpException
if (response.StatusCode != HttpStatusCode.OK)
throw new ApiException(result);
// Return our result if there are no errors
return result;
}
This allowed me to capture the ApiError and handle the response differently than with any other exception.

Stop Google OAuth reprompting for permissions

I have Google authentication working in my app through OAuthWebSecurity. Every time I login, Google re-prompts for permissions. How can I prevent Google from re-prompting every time?
I am currently using a custom OpenIdClient to get extra data from Google (source), but this also happens with the default client.
internal static class AuthConfig
{
public static void RegisterOpenAuth(ConfigurationWrapper configuration)
{
//OAuthWebSecurity.RegisterGoogleClient(); // using this has the exact same problem as my custom code
OAuthWebSecurity.RegisterClient(new GoogleCustomClient(), "Google", null);
}
}
/// <summary>
/// Represents Google OpenID client.
/// </summary>
public class GoogleCustomClient : OpenIdClient
{
#region Constructors and Destructors
public GoogleCustomClient()
: base("google", WellKnownProviders.Google)
{
}
#endregion
#region Methods
/// <summary>
/// Gets the extra data obtained from the response message when authentication is successful.
/// </summary>
/// <param name="response">
/// The response message.
/// </param>
/// <returns>A dictionary of profile data; or null if no data is available.</returns>
protected override Dictionary<string, string> GetExtraData(IAuthenticationResponse response)
{
var fetchResponse = response.GetExtension<FetchResponse>();
if (fetchResponse == null) return null;
var extraData = new Dictionary<string, string>
{
{"email", fetchResponse.GetAttributeValue(WellKnownAttributes.Contact.Email)},
{"country", fetchResponse.GetAttributeValue(WellKnownAttributes.Contact.HomeAddress.Country)},
{"firstName", fetchResponse.GetAttributeValue(WellKnownAttributes.Name.First)},
{"lastName", fetchResponse.GetAttributeValue(WellKnownAttributes.Name.Last)}
};
return extraData;
}
/// <summary>
/// Called just before the authentication request is sent to service provider.
/// </summary>
/// <param name="request">
/// The request.
/// </param>
protected override void OnBeforeSendingAuthenticationRequest(IAuthenticationRequest request)
{
// Attribute Exchange extensions
var fetchRequest = new FetchRequest();
fetchRequest.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
fetchRequest.Attributes.AddRequired(WellKnownAttributes.Contact.HomeAddress.Country);
fetchRequest.Attributes.AddRequired(WellKnownAttributes.Name.First);
fetchRequest.Attributes.AddRequired(WellKnownAttributes.Name.Last);
request.AddExtension(fetchRequest);
}
#endregion
}

Categories