I just started to get into ASP.net Web API (created MVC 4 project, Web API application, in .net 4.5).
I need to make custom handlers in particular. All I know is that we create one (inherit DelegatingHandler), and register it in App_Start/WebApiConfig.cs's Register function.
Where do we save such a handler (MyMessageHandler), though? No tutorial or book I've come tells me this. I tried to save it in the App_Start folder with the same namespace as WebApiConfig, but it says that MyMessageHandler cannot be found:
config.MessageHandlers.Add(new MyMessageHandler()); // MyMessageHandler is not found.
Where you store MyMessageHandler.cs doesn't matter at all, as long as you get your references in WebApiConfig right. I personally store them like project_root/MessageHandlers/MyMessageHandler.cs
Related
In previous version of asp.net the HostingEnvironment had MapPath method to get and store the path of the file but in ASP.net 5 I can't use it.
var filepath = HostingEnvironment.MapPath(#"~/Data/product.json");
Are you using RC1?
In RC1 it depends if you are writing a console or web app. In console apps, you cannot use DI anymore but PlatformServices.Default.
PlatformServices.Default.Application gives you access to the base path of your application for example. The type of the static property is IRuntimeEnvironment. And having the base path of your app, you can easily build the path to files you need...
If you are building a web app, you should be able to inject IRuntimeEnvironment to your startup and use it from there.
You have to add a reference to the Microsoft.Extensions.PlatformAbstractions package to all that stuff.
See also my post here for more details
Despite IHostingEnvironment provides MapPath() method you may also need UnmapPath() too. Another useful method might be IsPathMapped().
You can find all of them here: Reading a file in MVC 6.
And all of them, thanks to PlatformServices availability, work in Consolse, MVC, and ClassLib apps.
HTH
I'm trying to create a Web API project and a client-side web project, where the web project can access the API via ajax. Currently my project looks like this:
I saw this answer on here: Setting app a separate Web API project and ASP.NET app, which explains how the project url can be set to localhost:[port]/api.
But for ASP.NET 5 projects, the properties only have 3 tabs (as opposed to the several found in ASP.NET 4 projects):
What I'm wondering is:
Do I have to set this option somewhere else? (i.e project.json)
How would this work when I publish? Ideally I'd want [websiteURL]/api to serve up my API, whereas that link explicitly put localhost:8080.
Is having these as two projects a good idea? I could easily put API and web in the same project, but I like the separation of client-side and server-side logic.
Any help would be appreciated!
First Point:
Generally speaking in ASP.NET 5, the routing defaults are very good and should work out of the box without much in the way of configuration. You can use configuration and/or attribute based routing in your application (with a detailed overview of both here), although my personal preference is for the attributed approach. Provided you have the following line in your Startup.cs file (which you should have in a new project):
app.UseMvc();
you should be able to route requests to your api controllers in the fashion required (i.e. "/api/...") simply by using [Route] attributes as below (example taken from a standard generated ASP.NET 5 Web API application)
[Route("api/[controller]")]
public class ValuesController : Controller
{
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
}
The above example will route any GET request made to "/api/values".
While this approach can be used to handle requests made to your api, in order to deliver the files needed for your front end javascript application/single page app, you will need to enable static file serving. If you add the following to the Configure method in your Startup.cs class:
app.UseStaticFiles();
this will allow your application to serve those static files - by default, these are served from the ‘wwwroot’ folder, although this can be changed in the project.json file if required. The files needed for your front end app should then be added to this folder. A tutorial on serving static files can be found here.
Second Point:
In my experience this will not be an issue when you publish your website - provided your server is set up correctly, you will not need to include the port when making a request - navigating to [yourwebsitename]/api/... will suffice.
Third point:
In my opinion this entirely depends on how large the project is likely to grow, although preference and opinion will vary from developer to developer. Generally speaking, if the project will remain small in scope then keeping both in a single project is perfectly ok, as unnecessary complexity is reduced. However it is also very useful as you have pointed out, to maintain a separation of concerns between projects. So aside from the organisational advantage of your approach, the respective dependencies of the two projects are/will be kept separate also.
i write a simple webservice code in asp.net when i build the service and run the service it is working fine. when i try to access the webservice it is giving some problem , problem means i am not getting that method (webservice method). After completing writing the webserivce i take a asp.net page (.aspx) and in solution explorer i add a webservices and it is added successfully. but when i adding namespace it is not getting the service name ( i not able to add the namespace of websercice
I am not exactly sure what could be the problem but you should only need to do the following to use the web service:
// Look at what you named your web reference, in my example it is
// called MyWebService. Check your solution explorer for the actual name.
// This is the alias you should be using.
MyWebService.YourWebServiceName ws = new MyWebService.YourWebServiceName();
var result = ws.MyMethod(someparameter);
I'm working on a web site written using asp.net WebForms. I'd like two wrap test cases around some of the more interesting subroutines. How can I instantiate the class that comes from the .aspx file in my test project so I can manipulate it under nUnit?
Edit: What I really want to do is test the utility methods and event methods that are in the code-behind. I don't want to post to the page and read the response. I want to unit test the Methods, not the Page.
UPDATE Make sure you have setup your project to be an ASP.NET web project, not an asp.net web site. You can then mark your page class with the appropriate NUnit attributes and test the output dll for your project with NUnit.
Here is a Microsoft article that explains unit testing in asp.net:
http://msdn.microsoft.com/en-us/library/ms404696(VS.80).aspx
You can instantiate it just like any other type:
YourPage page = new YourPage();
Now getting the lifecycle to run will be quite another matter.
I'm trying to find a way to generate an enums class dynamically from lookup tables in a database and still have the convenience of a normal class (i.e. intellisense).
I've spent the past few hours trying to figure out how to get a custom BuildProvider to work inside an ASP.NET Web Application. The code works perfectly in a Web Site. I then found an article on MSDN that says
Adding a customized BuildProvider class to the Web.config file works in an ASP.NET Web site but does not work in an ASP.NET Web application project. In a Web application project, the code that is generated by the BuildProvider class cannot be included in the application. For more information, see Compiling Web Application Projects.
Does anyone know if it is possible to generate code dynamically and still be able to 'use' it at design time? Using a web site is not an option. I need to use a web project.
Thanks!!
what is the point of an enum class for a dynamic lookup table? your code references will always be static anyway...
if the initial population of the lookup table is static, make an enum for that and don't reference any other values in the code
If MSDN is saying you can't do it, I'd take another approach. Maybe write a small Console application that writes your Enums.cs file and run it through the "Pre-build event command line". Then, every time you build the web application, the Enums class gets recreated and should be accessible through Intellisense.
Haven't done this myself. Hope it helps.