Bug in my Web Api 2 call - GET returns 404 - http-status-code-404

I am writing a web api 2 method to get all credit cards based on someone being a student. I've written a new web api 2 controller but when I use POSTMAN I always get a 404.
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using CrazyCards.Web.Interfaces;
namespace CrazyCards.Web.Areas
{
public class CreditCardController : ApiController
{
private readonly ICreditCardDb _creditCardDb;
private readonly ICustomerDb _customerDb;
public CreditCardController(ICreditCardDb creditCardDb, ICustomerDb customerDb)
{
_creditCardDb = creditCardDb;
_customerDb = customerDb;
}
[HttpGet]
[Route("test")]
public IEnumerable<string> Test()
{
return new List<string>();
}
[HttpGet]
[Route("student/{customerId}")]
public HttpResponseMessage GetStudentCreditCards(int customerId)
{
var customer =
_customerDb.Customers.FirstOrDefault(x => x.Id == customerId);
if (customer != null && customer.EmploymentStatus == "Student")
{
var creditCards = _creditCardDb.CreditCards.Where(x => x.CardName == "Student");
return Request.CreateResponse(HttpStatusCode.OK, creditCards, Configuration.Formatters.JsonFormatter);
}
return Request.CreateResponse(HttpStatusCode.OK, new ArrayList(), Configuration.Formatters.JsonFormatter);
}
}
}
This is my localhost url: http://localhost:30046/api/creditcard/student/1
And a screenshot of postman

Unless I'm missing something, the url to your GetStudentCreditCards will be http://localhost:30046/student/{customerId} (mapped via the [Route("student/{customerId}")] attribute).
If you've got conventional routing on as well, you'll probably also find it at http://localhost:30046/api/creditcard/?customerId={customerId} (e.g http://localhost:30046/api/creditcard/?customerId=1 )

Related

ABPFramwork - Remove api from layer application in swagger

I have created a project using abpframwork. When running swagger, swagger receives the function in the application layer is a api. I don't want that. Can you guys tell me how to remove it in swagger
Code in Application Layer
public class UserService : AdminSSOAppService, ITransientDependency, IValidationEnabled, IUserService
{
IUserRepository _userRepository;
private readonly ILogger<UserService> _log;
public UserService(IUserRepository userRepository,
ILogger<UserService> log
)
{
_userRepository = userRepository;
_log = log;
}
public async Task<List<UserDto>> GetList()
{
var list = await _userRepository.GetListAsync();
return ObjectMapper.Map<List<User>, List<UserDto>>(list);
}
public async Task<UserDto> GetUserById(int Id)
{
var user = await _userRepository.GetAsync(c=>c.Id == Id);
return ObjectMapper.Map<User, UserDto>(user);
}
}
Code in HttpApi Layer
[Area(AdminSSORemoteServiceConsts.ModuleName)]
[RemoteService(Name = AdminSSORemoteServiceConsts.RemoteServiceName)]
[Route("api/user/user-profile")]
public class UserController : ControllerBase, IUserService
{
private readonly IUserService _userAppService;
public UserController(IUserService userAppService)
{
_userAppService = userAppService;
}
[HttpGet]
[Route("get-list-httpapi")]
public Task<List<UserDto>> GetList()
{
return _userAppService.GetList();
}
[HttpGet]
[Route("get-by-id-httpapi")]
public Task<UserDto> GetUserById(int Id)
{
return _userAppService.GetUserById(Id);
}
}
I can suggest a workaround as to enable only the APIs you need to appear on swagger (though the ones that don't appear anymore will still be available for consumption).
I would suggest you add a configuration part in your *.Http.Api project module inside your ConfigureSwaggerServices, like so:
context.Services.AddSwaggerGen(options =>
{
options.DocInclusionPredicate(
(_, apiDesc) =>
apiDesc
.CustomAttributes()
.OfType<IncludeInSwaggerDocAttribute>()
.Any());
});
And for the attribute, it would be very simple, like so:
[AttributeUsage(AttributeTargets.Class)]
public class IncludeInSwaggerDocAttribute : Attribute
{
}
This will let you achieve what you want, however I still recommend reading the doc carefully to be able to implement DDD.

xamarin.forms: retrieving existing data from .db file

In Xamarin Forms 4.3, I'm following this "Local Databases" doc: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/data/databases
This doc explains how to create and use a new sqlite db, but how do I go about in loading and displaying data in an existing .db file with the end purpose of transferring it into a new local sqlite database?
using SQLite;
namespace TruckExample.Models
{
public class Truck
{
[PrimaryKey, AutoIncrement]
public int TruckNo { get; set; }
public string TruckName { get; set; }
}
}
//////////////////////////////////////////////
using System.Collections.Generic;
using System.Threading.Tasks;
using SQLite;
using TruckExample.Models;
namespace TruckExample.Data
{
public class TruckDatabase
{
readonly SQLiteAsyncConnection _database;
public TruckDatabase(string dbPath)
{
_database = new SQLiteAsyncConnection(dbPath);
_database.CreateTableAsync<Truck>().Wait();
}
public Task<List<Truck>> GetTrucksAsync()
{
return _database.Table<Truck>().ToListAsync();
}
}
}
////////////////////////////////////////////////
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using TruckExample.Data;
using System.IO;
[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
namespace TruckExample
{
public partial class App : Application
{
static TruckDatabase database;
public static TruckDatabase Database
{
get
{
if(database == null)
{
database = new TruckDatabase(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Truck.db"));
}
return database;
}
}
public App()
{
InitializeComponent();
MainPage = new NavigationPage(new MainPage());
}
}
}
/////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace TruckExample
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MainPage : ContentPage
{
public MainPage ()
{
InitializeComponent ();
}
protected override async void OnAppearing()
{
base.OnAppearing();
listView.ItemsSource = await App.Database.GetTrucksAsync();
Debug.Write(App.Database.GetTrucksAsync());
}
}
}
For others, helpful resources:
Implementation by Hameed Kunkanoor: "Creating a sqlite databse and storing your data in your android and ios application in Xamarin Forms" https://medium.com/#hameedkunkanoor/creating-a-sqlite-databse-and-storing-your-data-in-your-android-and-ios-application-in-xamarin-2ebaa79cdff0
(Refer to his git if needed)
Explanation/response by Matthewrdev: Use a local database in Xamarin
To improve the implementation -- Brandon Minnick: "Xamarin: Efficiently Using a SQLite Database" https://codetraveler.io/2019/11/26/efficiently-initializing-sqlite-database/

How to validate if email attribute is unique in an ASP .NET CORE API?

I was needing to make a post or put a validation on the server side to check if the email is unique.
In the research I have always done the example was a traditional MVC application and never an api.
In many cases I saw that the [Remote] https://learn.microsoft.com/pt-br/aspnet/core/mvc/models/validation?view=aspnetcore-2.2#remote-attribute . I tried to implement according to the documentation, but debugging verified that the function in the controller is neither called.
User.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.AspNetCore.Mvc;
namespace Base.Models
{
[Table("users")]
public partial class User
{
...
[Required]
[EmailAddress]
[Remote(action: "VerifyEmail", controller: "UserController",ErrorMessage="Email already in use")]
[Column("email", TypeName = "varchar(254)")]
public string Email { get; set; }
...
}
}
UserController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Base.Models;
namespace Base.Controllers
{
[Route("api/users")]
[ApiController]
public class UserController : Controller
{
...
[AcceptVerbs("Get")]
public IActionResult VerifyEmail(string email)
{
//forcing it to go wrong
return Json($"Email {email} is already in use.");
}
...
}
}
Anyone have any idea how to implement this?
Seeing that it was not progressing, I decided to do a personalized validation.
EmailUserUniqueAttribute.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using Base.Models;
namespace Core.Models
{
public class EmailUserUniqueAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(
object value, ValidationContext validationContext)
{
var _context = (AppDbContext)validationContext.GetService(typeof(AppDbContext));
var entity = _context.Users.SingleOrDefault(e => e.Email == value.ToString());
if (entity != null)
{
return new ValidationResult(GetErrorMessage(value.ToString()));
}
return ValidationResult.Success;
}
public string GetErrorMessage(string email)
{
return $"Email {email} is already in use.";
}
}
}
User.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.AspNetCore.Mvc;
namespace Base.Models
{
[Table("users")]
public partial class User
{
...
[Required]
[EmailAddress]
[EmailUserUnique]
[Column("email", TypeName = "varchar(254)")]
public string Email { get; set; }
}
}
...
It works, but I don't know if this is the best way to do this.
I cant comment cause I lack the sufficient reputation, and if I had time I would had gone and verified but I think your issue here is the [Route("api/users")].
You are giving your decorator the Controller name but that controller is behind a different route. Easy debug of that will be to remove the Route temporarily OR put the "VerifyEmail" action to another controller that you arent altering its route.

Selenium Webdriver C# - Running more than one test case in VS 2017 from Test Suite

I have been working on Page Object Framework which will have categorized test suites depending on the page. I have followed all steps in order to build a decent framework.
My each Unit Test, contains one method which follows simple steps. So far I have been able to create a few automated test cases. The issue began when I wanted to run more than one test case from the test suite. I have one chrome web driver instance which is in a separate class. Below is an example:
using System;
using System.Dynamic;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
namespace SystemUiAutomationTestFramework
{
public static class Browser
{
private static readonly IWebDriver _webDriver = new ChromeDriver(Properties.Settings.Default.ChromePathDriver);
public static IWebDriver WebDriver {
get { return _webDriver; }
}
public static ISearchContext Driver
{
get { return _webDriver; }
}
public static string Url
{
get { return _webDriver.Url; }
}
public static string Title
{
get { return _webDriver.Title; }
}
public static void Goto(string url)
{
_webDriver.Manage().Window.Maximize();
_webDriver.Url = url;
}
public static void Close()
{
_webDriver.Quit();
}
}
}
Each test case when it is run is independent and as good practice shows, there should be no test order implementation because it generates flows. I will place two examples which check simple login operation and login validation.
Below you can find a class for the login page:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.PageObjects;
using OpenQA.Selenium.Support.UI;
namespace SystemUiAutomationTestFramework
{
public class LoginPage
{
static string Url = "http://..";
private static string LoginUrl = "http://...";
private static string PageTitle = "System title page";
private static string LoginPlaceholder = "Login";
private static string PswdPlaceholder = "Password";
private static string ButtonText = "Sign in";
private static string ErrorMessage = "Wrong login or password";
/* LoginPage Elements */
[FindsBy(How = How.Id, Using = "inputLogin")]
private IWebElement inputLogin;
[FindsBy(How = How.Id, Using = "inputPassword")]
private IWebElement inputPassword;
[FindsBy(How = How.TagName, Using = "button")]
private IWebElement loginButton;
[FindsBy(How = How.ClassName, Using = "errorMsg")]
private IWebElement errorMessage;
/*----------------------------------------*/
public void Goto()
{
Browser.Goto(Url);
}
public bool IsAtLoginPage()
{
return Browser.Url == LoginUrl;
}
public bool IsAtLoginPageTitle()
{
return Browser.Title == PageTitle;
}
public bool IsAtLoginField()
{
return inputLogin.GetAttribute("placeholder") == LoginPlaceholder;
}
public bool IsAtPswdField()
{
return inputPassword.GetAttribute("placeholder") == PswdPlaceholder;
}
public bool IsAtLoginButton()
{
return loginButton.Text == ButtonText;
}
public void InputCredentials(string userName, string userPassword)
{
inputLogin.SendKeys(userName);
inputPassword.SendKeys(userPassword);
loginButton.Click();
}
public void WaitErrorMessage()
{
WebDriverWait wait = new WebDriverWait(Browser.WebDriver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.ClassName("errorMsg")));
}
public bool IsAtErrorMessage()
{
return errorMessage.Text == ErrorMessage;
}
public void ReportPageSuccessLogin()
{
Console.WriteLine("Application Url checked");
Console.WriteLine("Application Title checked");
Console.WriteLine("Login Field present");
Console.WriteLine("Password Field checked");
Console.WriteLine("Sign in button checked");
Console.WriteLine("Login ssuccess");
}
public void ReportPageValidationTest()
{
ReportPageSuccessLogin();
Console.WriteLine("Fake Credentials entered");
Console.WriteLine("Login Button Pressed");
Console.WriteLine("Login or Password validation message displayed: " + errorMessage.Displayed);
}
}
}
I also have an API class for Pagefactoring. As an example:
public static class Pages
{
public static LoginPage LoginPage
{
get
{
var loginPage = new LoginPage();
PageFactory.InitElements(Browser.Driver, loginPage);
return loginPage;
}
}
Now for the test cases, below an example when a user is on the login page, all elements are displaed, logs into the system, system checks if the user has logged into and closes the instance.
using System;
using System.Runtime.Remoting.Channels;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SystemUiAutomationTestFramework;
namespace TestSuite.LoginPage
{
[TestClass]
public class LoginPageTest
{
[TestMethod]
public void Can_Go_LoginPage()
{
Pages.LoginPage.Goto();
Assert.IsTrue(Pages.LoginPage.IsAtLoginPage());
Assert.IsTrue(Pages.LoginPage.IsAtLoginPageTitle());
Assert.IsTrue(Pages.LoginPage.IsAtLoginField());
Assert.IsTrue(Pages.LoginPage.IsAtPswdField());
Assert.IsTrue(Pages.LoginPage.IsAtLoginButton());
Pages.LoginPage.InputCredentials(SettingsService.Username, SettingsService.Userpassword);
Pages.HomePage.IsAtHomePage();
Pages.LoginPage.ReportPageSuccessLogin();
}
[TestCleanup]
public void CleanUp()
{
Browser.Close();
}
}
}
The other one just validates the login page by providing fake login and password and if the error message is correctly displayed the test case finishes also by closing the instance.
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SystemUiAutomationTestFramework;
namespace TestSuite.LoginPage
{
[TestClass]
public class LoginPageValidationTest
{
[TestMethod]
public void Can_Validate_LoginPage()
{
Pages.LoginPage.Goto();
Assert.IsTrue(Pages.LoginPage.IsAtLoginPage());
Assert.IsTrue(Pages.LoginPage.IsAtLoginPageTitle());
Assert.IsTrue(Pages.LoginPage.IsAtLoginField());
Assert.IsTrue(Pages.LoginPage.IsAtPswdField());
Assert.IsTrue(Pages.LoginPage.IsAtLoginButton());
Pages.LoginPage.InputCredentials("abcd.efgh", "test123");
Pages.LoginPage.WaitErrorMessage();
Assert.IsTrue(Pages.LoginPage.IsAtErrorMessage());
Pages.LoginPage.ReportPageValidationTest();
}
[TestCleanup]
public void CleanUp()
{
Browser.Close();
}
}
}
Now the issue happens when I try those two test cases from the Login Page Test Suite, one finishes as passed the other one as failed. From the error message, I understand that it is because the other test case is trying to use an instance of the Webdriver which is already running.
I would like to know your opinion/guides/solution how can I solve this problem. I apologize for the long post but I thought that if I place my code it will be easy for you to understand my issue.
Best regards and thank you for your answers or linking me to the topic which either a duplicate of my issue or there is a solution already for it.
I was able to solve the issue on my own. I have created an
property for my driver class and an Initialize method which is called each time a test class is called. Also refactored my code to be more flexible.

Nancy fx how to use in Windows Forms

Here I have a simple WinForm app which has a NancyFx service all working fine: I use a Person object which implements the IPerson interface. The nancyModule has a ctor with a parameter of IPerson and in the post route of the nancyModule I use the this.Bind(); If I want to display the person on the form how do I do it?
using System;
using System.Windows.Forms;
using Microsoft.Owin.Hosting;
using Nancy;
using Nancy.ModelBinding;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private IDisposable dispose;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string uri = "http://localhost:8080/";
dispose = WebApp.Start<Startup1>(uri);
}
}
public interface IPerson
{
String Name { get; set; }
}
public class Person : IPerson
{
public String Name { get; set; }
}
public class nancyModule : NancyModule
{
public nancyModule(IPerson person)
{
Post["/data"] = _ =>
{
person = this.Bind<Person>();
//HOW DO I DISPLAY THE person ON THE FORM UI
return HttpStatusCode.OK;
};
}
}
}
If you want to display the person data on the form then you need to call your REST API from your Win Forms application. Grab the response and output the results. Simply put, this is how you can achieve this.
I haven't used async and await keywords which ideally you would but
for brevity I have omitted this.
Firstly, I removed the dependency of IPerson from your module as this isn't a dependency as such but an output from your POST. With that minor adjustment, it looks like this:
If you still feel strongly about IPerson being a dependency then simply leave it and the code will still work as expected.
public class PersonModule : NancyModule
{
public PersonModule()
{
this.Post["/data"] = args => this.AddPerson();
}
private Negotiator AddPerson()
{
var person = this.Bind<Person>();
return this.Negotiate
.WithStatusCode(HttpStatusCode.Created)
.WithContentType("application/json")
.WithModel(person);
}
}
Now from your Win Forms application simply call the API via the HttpClient, like this:
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var person = new Person { Name = "Foo Bar" };
var serializer = new JavaScriptSerializer();
var response = client.PostAsync(
"http://localhost:8080/data",
new StringContent(serializer.Serialize(person), Encoding.UTF8, "application/json")).Result;
var result = new JavaScriptSerializer().Deserialize<Person>(response.Content.ReadAsStringAsync().Result);
TextBox1.Text = result.Forename;
}
Purest's out there will mention 3rd party libraries such as Json.NET
and Service Stack which allows for easier serialization and
deserialization but again for simplicity in this example I am using
out of the box features.

Resources