I have one project with one domain and another project with a different domain. I want to have a button on the first project which links to the second project. How can I accomplish this? I currently only know how to change the controller and action with an href
There are no built-in helpers allowing you to generate links across different ASP.NET projects. The url helpers are intended to be used only for generating links to internal controller actions. ASP.NET routing works only for the current project.
You will have to use an absolute url or write custom helpers that could for example use some base url that you would configure in your web.config that will be concatenated with the actual relative url you need to reach.
Related
I am learning ASP.NET coming from a Node.js background.
When I create a new MVC project, I can choose to have built-in register/login.
This gives me the following views, where I can register and login.
But I am confused as I cannot find the corresponding controller or views in the directory, which is problematic if I want to customize the behaviour.
Can someone shed the light on how this works and where are the controller and view? Thanks.
It is not in project folder. UI is loaded from Microsoft.AspNetCore.Identity.UI library.
You can check it's code in below URL. you can understand how to configure by looking at code.
https://github.com/dotnet/aspnetcore/tree/main/src/Identity/UI/src
They are not using MVC style, they are using Razor pages with code behind C# model.
You can provide your own UI by using attribute like [IdentityDefaultUI(typeof(LoginModel<>))] on your page model.
Reference:
https://github.com/dotnet/aspnetcore/blob/main/src/Identity/UI/src/Areas/Identity/Pages/V5/Account/Login.cshtml.cs
The controller folder contains the controller file in which you will see the server side(C#) code it will receive request from view and process on it and send back to view. And views folders contains the html code that get input from user. In this project structure, you will see shared folder that contains generic view files and _loginpartial.cshtml is login view.
We have legacy code to maintain and, to solve a specific customer customization problem, we want to route calls to some files to other files. That is, when the app calls a particular ASPX, it will end up hitting another ASPX.
If you call:
www.foo.com/admin/admin.aspx
It will actually hit:
www.foo.com/customizations/customer1/admin/admin.aspx
This is not a good design but this is legacy code. We just want to solve this.
We are using the System.Web.Routing framework to solve it. This works fine when you set RouteExistingFiles to true, except for static files (CSS, JavaScript and Images).
When I first tried it, it retrieved this error:
There is no build provider register for the extension '.css'.
So I did register a build provider in the web.config file for the .css extension. I used this build provider: PageBuilderProvider because someone recommended it in the internet.
It works! But the CSS is being served with text\html content type.
How do I achieve this?
TL;DR: I want to use routes in ASP.NET Web Forms to make a call for a specific CSS file to actually retrieve another one. A customer needs this for customization.
Try coding a HttpHandler. I had to do something similar but for PDF files, I coded a custom HttpHandler in the end - works very well. You can even set the content type in the HttpHandler code and have a pattern matched path the handler will be used for in the web.config. You can also configure it in web.config not to execute if the path does not point to an existing file e.g. so a 404 is returned without having to code that in the handler itself. I can't post my code (VB.NET) ATM because I'm using a tablet but google search for tutorials. You will also probably need to use the TransmitFile function to actually write out the css file. Is it a web forms project or web site? If its a web site there is a special way of registering the HttpHandler in the web.config.
I want to learn is it possible to add additional folder to Controller folder. My reason is pretty simple: I want to divide my project administration and client sides.
Example: I have a controller named Post that has actions Index, Details, Delete, Create, Edit. I want to make one controller as user controller that will consist of Index, Details and another controller as admin controller that will consist of Delte, Create, Edit. Then I will be able to easy distinguish what is what and put admin validation on whole admin class.
Another reason is that I want my url for administrating my site to look like /admin/post/delete, not /post/delete.
So is it possible, and if so then what would be the best way to implement this?
Sound like you want to use MVC Areas? http://www.c-sharpcorner.com/UploadFile/b19d5a/areas-in-Asp-Net-mvc3/
It's just a convention on placing controllers in Controllers folder.
Actually MVC finds controller in current loaded assemblies.
You can place them even in other assemblies.
So, fell free to create additional folders inside Controllers
If you are using Ruby on Rails, yes, you can. In your routes files, config/routes.rb, add this:
map.namespace :admin do |admin|
admin.resources :posts
end
Go to your terminal and navigate to your project, run rake routes. Now you get your posts controller under admin namespace... and your url will be:
.../admin/posts
We liked the approach to have all the pages regarding support - for example - under http://www.company.com/support. After migrating to ASP.NET MVC 3 and trying this we can run every type of page but not inside the same folder.
Is there any workaround for this?
Thanks.
If you need to mix MVC pages and non-MVC pages in the same folder, here's some tips:
Remove the default route "/{controller}/{action}/{id}" and make routes for each MVC page. That way any request that isn't caught by a route falls through to the "old" request handling.
A return View(); method call in a controller looks for a view in a folder named as the controller in the Views folder, so specify the name of the view, e.g. return View("/support/index");.
Note that the MVC views doesn't actually have to be in the folder support, you can put them anywhere you like, it's the routes that determine which URLs are handled by MVC.
I'm using ASP.NET 4 Web forms routing, for example like this:
routes.MapPageRoute("page-browse", "{Language}/{Label}", "~/Default.aspx")
So the webadress could look like: http://localhost/mywebsite/eng/home
In the root of my website I have a folder "Images".
Image display works when I'm in the root of my website, e.g. by using http://localhost/mywebsite/default.aspx
But when using routing it doesn't work, because the image relative url will look at http://localhost/mywebsite/eng/images instead of http://localhost/mywebsite/images
Is there a way to prevent this using ASP.NET 4 Routing mechanism? Or is the only way to use absolute url's to images?
Two things you could try.
1) Set RouteExistingFiles to false. This will stop routing on any file that the server matches as already existing. Only "virtual" urls that match a route will actually be routed:
routes.RouteExistingFiles = false;
2) Use a StopRoutingHandler route. For example, this would stop routing on all jpgs. You could also set it up to ignore the entire images directory.
routes.Add(new Route("*\.jpg", new StopRoutingHandler()));
Create an IHttpHandler implementation that locates the file's absolute path from the virtual path and renders it as a filestream.
Create an IRouteHandler implementation that returns the http handler I just described if the file has an image extension, or uses the default routing mechanism otherwise.
Register the route handler to be used in the route.
How are you referencing your images?
You should just be able to use a virtual path to the site root, using a prefix of '/' (unless I'm completely misunderstanding the question)
e.g.
<img src="/mywebsite/images/image.jpg" />